[英]How can I set exactly the limits for axes in ggplot2 R plots?
Making a plot with ggplot, I wish to set my axis exactly . 用ggplot绘制图,我希望精确地设置轴。 I am aware that I can set the plot range (eg for the x-axis I specified limits from 2 to 4) with
coord_cartesian()
but that leaves a bit of space to the left and right of the range I specify : 我知道我可以使用
coord_cartesian()
设置绘图范围(例如,对于我指定的x轴限制从2到4),但是在我指定的范围的左侧和右侧留出一些空间 :
Code for the MWE above: 上面的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)
My desired result is a plot where the displayed area is exactly between the limits I specify. 我想要的结果是显示区域正好在我指定的限制之间的绘图。
I am aware of How to set limits for axes in ggplot2 R plots? 我知道如何在ggplot2 R图中设置轴的限制? but it does not answer my question, as it produces the undesired result above, or cuts out observations (with the
limits
argument to scale_x_continuous
). 但这并不能回答我的问题,因为它会在上面产生不希望的结果,或者削减观察结果(使用
scale_x_continuous
的limits
参数)。 I know I could tinker with setting a smaller range for limits, but I am looking for a clean result. 我知道我可以修改限值的较小范围,但是我正在寻找一个干净的结果。 At the least I would like to know by how much the actual range differs from the one I specify, so that I could adapt my limits accordingly.
至少我想知道实际范围与我指定的范围相差多少,以便我可以相应地调整自己的限制。
Add expand = FALSE
: 添加
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.