用ggplot制作一个情节,我希望准确设置我的轴.我知道我可以设置绘图范围(例如,对于x轴我指定的限制从2到4),coord_cartesian()
但是在我指定的范围的左侧和右侧留下一点空间:
以上MWE的代码:
library(ggplot2)
data.mwe = cbind.data.frame(x = 1:5, y = 2:6)
plot.mwe = ggplot(data = data.mwe, aes(x=x,y=y)) + geom_line() + coord_cartesian(xlim = c(2,4))
print(plot.mwe)
Run Code Online (Sandbox Code Playgroud)
我想要的结果是一个图表,其中显示的区域正好在我指定的限制之间.
我知道
如何在ggplot2 R图中设置轴的限制?
但它没有回答我的问题,因为它产生了上面不希望的结果,或者削减了观察结果(带有limits
参数scale_x_continuous
).我知道我可以修改设置较小的限制范围,但我正在寻找一个干净的结果.至少我想知道实际范围与我指定的范围有多大,以便我可以相应地调整我的限制.
添加expand = FALSE
:
library(ggplot2)
data.mwe = data.frame(x = 1:5,
y = 2:6)
ggplot(data.mwe, aes(x, y)) +
geom_line() +
coord_cartesian(xlim = c(2, 4),
expand = FALSE)
Run Code Online (Sandbox Code Playgroud)