简体   繁体   English

如何在ggplot2 R图中精确设置轴的极限?

[英]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_continuouslimits参数)。 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.

相关问题 如何在 ggplot2 R 图中设置轴的限制? - How to set limits for axes in ggplot2 R plots? R和ggplot 2:如何在ggplot2 R图中设置轴的分位数限制? - R and ggplot 2: How to set quantile limits for axes in ggplot2 R plots? 组合ggplot2对象时在拼凑中设置轴限制 - set axes limits in patchwork when combining ggplot2 objects R:如何将全局/跨 X 轴和 Y 轴添加到拼凑而成的 ggplot 图网格? - R: How can I add global/spanning X and Y axes to a grid of ggplot plots with patchwork? 如何在 plot 和 R 和 ggplot2 中使用 2 个不同的 y 轴? - How can I plot with 2 different y-axes in R with ggplot2? 如何将每个图指向正确的y轴(许多图,两个y轴,带ggplot2的R) - How to point each plot to correct y axis (many plots, two y axes, in R with ggplot2) R:{ggplot2}:如何/我可以独立调整 facet_grid plot 上的 x 轴限制? - R: {ggplot2}: How / Can I independently adjust the x-axis limits on a facet_grid plot? 如何在ggplot2 / R的绘图轴限制之外添加垂直线和水平文本? - How can I add vertical lines and horizontal text out of the plot axis limits in ggplot2/R? ggplot2 在散点图中给了我 R 中的蓝调,我该如何设置光谱颜色? - ggplot2 gives me the blues in R in scatter plots, how do I do a spectral color set? 如何设置 ggplot2 散点图中的点大小以匹配轴的比例? - How can I set the point size in a ggplot2 scatterplot to match the scale of the axes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM