R plot.default 中的 x 轴太短
x axis is too short in R plot.default
我的x轴太短了。
d <- data.frame(x = c(120,200,300), y = rep(1,3))
plot(d$x,
d$y,
xlim = c(min(d$x), max(d$x)),
axes = FALSE,
xlab = "",
ylab = "")
axis(1, lwd = 2)
显然这并不理想。我知道有许多针对单个绘图实例的解决方案。但是,我以编程方式生成了许多具有不同 x 值的此类图表。因此,我需要一个通用且可直接适用于不同 x 值的解决方案。
下面是一些头脑风暴代码:
#This works but the tick marks are ugly. I also can't control lwd of axis
plot(d$x,
d$y,
yaxt="n",
frame.plot = FALSE,
xlab = "",
ylab = "",
xaxp = c(120, 300, 50))
#this is the solution for this particular case, however, it would not work in general
plot(d$x,
d$y,
xlim = c(min(d$x)-20, max(d$x)),
axes = FALSE,
xlab = "",
ylab = "")
axis(1, lwd = 2)
有什么想法吗?
这是一个使用 ggplot2
进行绘图的选项。下面的代码将 x 范围设置为数据范围下方和上方最接近的 50 的倍数,并在每 50 的倍数处放置刻度线。不同地块的数据范围。
library(ggplot2)
library(gridExtra) # For grid.arrange function
xfun
计算最小和最大 x 限制。 tick.dist
设置图表中刻度线之间的距离。默认设置为 50。因此,默认情况下,当 which="min"
、xfun
returns 小于 value
的 50 的最大倍数时。当which="max"
、xfun
、returns大于value
的50的大倍数时。
xfun = function(value, which, tick.dist=50) {
# Calculate lower x-limit
if(which=="min") {
return(value - value %% tick.dist)
}
# Calculate upper x-limit
if(which=="max") {
return(value + (tick.dist - value %% tick.dist))
}
}
现在我们创建一个自定义主题,仅绘制点标记和 x 轴:
my_theme = list(theme_bw(),
theme(panel.border=element_blank(),
axis.line=element_line(),
axis.line.y=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank()),
labs(y="",x=""))
现在我们为四组不同的 x 值创建绘图,使用 xfun
设置 x 限制。
# Try out various ranges for the x-values
x_vals = c(-23, 56, 80, 123)
# Set distance between ticks and x-axis padding
tick.dist=50
pad = 5
# List to store the plots
p.list=list()
# Create a plot for each element of x_vals
for (i in 1:length(x_vals)) {
# Create fake data
d <- data.frame(x=runif(10, x_vals[i], x_vals[i] + 220), y = rep(3,10))
# Set x limits
xmin = xfun(min(d$x), "min", tick.dist)
xmax = xfun(max(d$x), "max", tick.dist)
# Plot
p.list[[i]] = ggplot(d, aes(x,y)) +
geom_point() +
# Set the exact x-axis range
coord_cartesian(xlim=c(xmin - pad, xmax + pad)) +
# Set major tick values
scale_x_continuous(breaks=seq(-1000,1000, tick.dist)) +
my_theme
}
# Plot all the graphs on a single "page"
do.call(grid.arrange, p.list)
这是一种使用基本 R 图形的方法。逻辑类似于 ggplot2
答案。下面的代码将 x 范围设置为数据范围下方和上方最接近的 50 的倍数,并在每 50 的倍数处放置刻度线。不同地块的数据范围。
xfun
计算最小和最大 x 限制。 tick.dist
设置图表中刻度线之间的距离。默认设置为 50。因此,默认情况下,当 which="min"
、xfun
returns 小于 value
的 50 的最大倍数时。 which="max"
、xfun
、returns大于value
的50的大倍数时。
xfun = function(value, which, tick.dist=50) {
# Calculate minimum x-limit
if(which=="min") {
return(value - value %% tick.dist)
}
# Calculate maximum x-limit
if(which=="max") {
return(value + (tick.dist - value %% tick.dist))
}
}
现在我们创建四个样本地块。
par(mfrow=c(2,2))
# Try out various ranges for the x-values
x_vals = c(-23, 56, 80, 123)
# Set distance between tick marks
tick.dist=50
for (i in 1:length(x_vals)) {
# Create fake data
d <- data.frame(x=runif(10, x_vals[i], x_vals[i] + 220), y = rep(3,10))
# Set x limits and number of tick marks
xmin = xfun(min(d$x), "min", tick.dist)
xmax = xfun(max(d$x), "max", tick.dist)
nticks = as.integer((xmax - xmin)/tick.dist)
# Plot
plot(d$x, d$y, xaxt="n", yaxt="n", frame.plot = FALSE,
xlab = "", ylab = "", xlim=c(xmin, xmax))
# xaxp controls location of min and max x-axis tick marks
# as well as the total number of tick marks.
# See ?par("xaxp") for more info.
axis(1, lwd = 2, xaxp = c(xmin, xmax, nticks))
}
我的x轴太短了。
d <- data.frame(x = c(120,200,300), y = rep(1,3))
plot(d$x,
d$y,
xlim = c(min(d$x), max(d$x)),
axes = FALSE,
xlab = "",
ylab = "")
axis(1, lwd = 2)
显然这并不理想。我知道有许多针对单个绘图实例的解决方案。但是,我以编程方式生成了许多具有不同 x 值的此类图表。因此,我需要一个通用且可直接适用于不同 x 值的解决方案。
下面是一些头脑风暴代码:
#This works but the tick marks are ugly. I also can't control lwd of axis
plot(d$x,
d$y,
yaxt="n",
frame.plot = FALSE,
xlab = "",
ylab = "",
xaxp = c(120, 300, 50))
#this is the solution for this particular case, however, it would not work in general
plot(d$x,
d$y,
xlim = c(min(d$x)-20, max(d$x)),
axes = FALSE,
xlab = "",
ylab = "")
axis(1, lwd = 2)
有什么想法吗?
这是一个使用 ggplot2
进行绘图的选项。下面的代码将 x 范围设置为数据范围下方和上方最接近的 50 的倍数,并在每 50 的倍数处放置刻度线。不同地块的数据范围。
library(ggplot2)
library(gridExtra) # For grid.arrange function
xfun
计算最小和最大 x 限制。 tick.dist
设置图表中刻度线之间的距离。默认设置为 50。因此,默认情况下,当 which="min"
、xfun
returns 小于 value
的 50 的最大倍数时。当which="max"
、xfun
、returns大于value
的50的大倍数时。
xfun = function(value, which, tick.dist=50) {
# Calculate lower x-limit
if(which=="min") {
return(value - value %% tick.dist)
}
# Calculate upper x-limit
if(which=="max") {
return(value + (tick.dist - value %% tick.dist))
}
}
现在我们创建一个自定义主题,仅绘制点标记和 x 轴:
my_theme = list(theme_bw(),
theme(panel.border=element_blank(),
axis.line=element_line(),
axis.line.y=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank()),
labs(y="",x=""))
现在我们为四组不同的 x 值创建绘图,使用 xfun
设置 x 限制。
# Try out various ranges for the x-values
x_vals = c(-23, 56, 80, 123)
# Set distance between ticks and x-axis padding
tick.dist=50
pad = 5
# List to store the plots
p.list=list()
# Create a plot for each element of x_vals
for (i in 1:length(x_vals)) {
# Create fake data
d <- data.frame(x=runif(10, x_vals[i], x_vals[i] + 220), y = rep(3,10))
# Set x limits
xmin = xfun(min(d$x), "min", tick.dist)
xmax = xfun(max(d$x), "max", tick.dist)
# Plot
p.list[[i]] = ggplot(d, aes(x,y)) +
geom_point() +
# Set the exact x-axis range
coord_cartesian(xlim=c(xmin - pad, xmax + pad)) +
# Set major tick values
scale_x_continuous(breaks=seq(-1000,1000, tick.dist)) +
my_theme
}
# Plot all the graphs on a single "page"
do.call(grid.arrange, p.list)
这是一种使用基本 R 图形的方法。逻辑类似于 ggplot2
答案。下面的代码将 x 范围设置为数据范围下方和上方最接近的 50 的倍数,并在每 50 的倍数处放置刻度线。不同地块的数据范围。
xfun
计算最小和最大 x 限制。 tick.dist
设置图表中刻度线之间的距离。默认设置为 50。因此,默认情况下,当 which="min"
、xfun
returns 小于 value
的 50 的最大倍数时。 which="max"
、xfun
、returns大于value
的50的大倍数时。
xfun = function(value, which, tick.dist=50) {
# Calculate minimum x-limit
if(which=="min") {
return(value - value %% tick.dist)
}
# Calculate maximum x-limit
if(which=="max") {
return(value + (tick.dist - value %% tick.dist))
}
}
现在我们创建四个样本地块。
par(mfrow=c(2,2))
# Try out various ranges for the x-values
x_vals = c(-23, 56, 80, 123)
# Set distance between tick marks
tick.dist=50
for (i in 1:length(x_vals)) {
# Create fake data
d <- data.frame(x=runif(10, x_vals[i], x_vals[i] + 220), y = rep(3,10))
# Set x limits and number of tick marks
xmin = xfun(min(d$x), "min", tick.dist)
xmax = xfun(max(d$x), "max", tick.dist)
nticks = as.integer((xmax - xmin)/tick.dist)
# Plot
plot(d$x, d$y, xaxt="n", yaxt="n", frame.plot = FALSE,
xlab = "", ylab = "", xlim=c(xmin, xmax))
# xaxp controls location of min and max x-axis tick marks
# as well as the total number of tick marks.
# See ?par("xaxp") for more info.
axis(1, lwd = 2, xaxp = c(xmin, xmax, nticks))
}