我有一个看似非常简单的问题,但找不到答案。我创建了这个情节,并且只想让两个外部点更接近中间。
#Sample code
x=1:3
y=c(-50,-70,-120)
plot(x,y)
我通过设置par(mar = c(5.1,9,4.1,9))尝试了此reducing the space between plotted points in plot( x, y) type=n,但这只会改变绘图的比例,但不会改变相对距离。我和qplot有同样的问题。请注意,我想用axis()设置我自己的刻度标签。
答案 0 :(得分:1)
您可以在x轴点的两侧添加一些填充。例如,这是一个负责填充的函数,并提供对轴断点数的控制:
x=1:3
y=c(-50,-70,-120)
# Function to plot with padding on either side of x-axis points.
# Padding is set with pad parameter equal to a fraction of the range of the x values.
# The ... argument allows you to pass additional arguments to plot, such as
# xlab, main, ylim, col, etc.
pad_plot = function(x, y, pad=0.4, n=5, ...) {
# Get range of x values
xrng = diff(range(x))
# Plot, but don't include axis, so that we can directly control the axis labels.
# Otherwise, plot will add axis breaks at 0, 4, and other values outside the
# range of the data.
plot(x,y, xlim = range(x) + c(-1,1)*pad*xrng, xaxt="n", ...)
# Add axis breaks and labels
axis(side=1, at=pretty(x, n))
}
par(mfrow=c(2,2))
pad_plot(x,y)
pad_plot(x,y, n=2, main="This is a title", pch=16, col="red")
pad_plot(x,y,pad=0.2, n=8)
pad_plot(x,y, pad=2)