我可以精确限制ggplot轴范围吗? [英] Can I limit ggplot axis range EXACTLY?

查看:40
本文介绍了我可以精确限制ggplot轴范围吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ggplot图中,我想在辅助轴上重新缩放y,以使最小y值等于0,最大y值等于100. 我是这样尝试的:

In a ggplot graph I want to rescale y on the secondary axis so that the smallest y value equals 0 and the highest equals 100. I tried it this way:

mydata <- data.frame(x = c(0, 1, 2, 3, 4, 5), 
                     y = c(20, 55, 69, 72, 73, 72))
break2 <- seq(0, 100, 10)

library(ggplot2)

ggplot(data = mydata, aes(x = x, y = y)) +
  ylim(20, 73) +
  geom_point(shape = 21, size = 5, color = "black", fill = "orange", stroke = 1) +
  scale_x_continuous(name = 'Number of Contacts') +
  scale_y_continuous(name = "Remembered (%)", 
                     sec.axis = sec_axis(trans = ~ (.-min(.)) * 100 / (max(.) - min(.)),
                                         name = "Remembered (Index)",
                                         breaks = break2))

可以看出,ggplot忽略了y限制为20-73的事实.它以较低的值开始,以左侧的较高值结束.因此,右侧的辅助轴错误地重新缩放:y=20对应于重新缩放的值>0.此外,y=73并不对应于预期的100.

As can be seen, ggplot ignores the fact that the y limit is 20-73. It starts with lower values and ends with higher values on the left. Accordingly the secondary axis on the right rescales wrongly: y=20 corresponds to a rescaled value > 0. Also, y=73 does not correspond to 100 as intended.

可以将y轴范围扩大,但是右边的0应该从y的最小值20开始.

It's acceptable to have y axis range enlarged, but 0 on the right should start at the lowest value of y which is 20.

有没有办法解决这个问题?还是有更好的方法来做到这一点?

Is there a way to fix that? Or is there a better approach to do that?

推荐答案

正如Axeman所指出的,在scale_y_continuous()中指定limits将限制y轴.另一种选择是在代码中添加coord_cartesian(ylim = c(20, 73)).

As Axeman noted, specifying limits in scale_y_continuous() would limit the y-axis. Another alternative is to add coord_cartesian(ylim = c(20, 73)) to your code.

两者之间的区别(与geom_point不相关)是scale_y_continuous(limits = c(...))会限制传递给ggplot进行绘图的值的范围,而coord_cartesian(ylim = c(...))会在绘制所有内容后限制可见范围.

The difference between the two (irrelevant for geom_point) is that scale_y_continuous(limits = c(...)) would restrict the range of values passed to ggplot for plotting, while coord_cartesian(ylim = c(...)) retricts the visible range after everything has been plotted.

ggplot2软件包速查表 RStudio很好地总结了这一点:

The ggplot2 package cheatsheet over at RStudio summarises this nicely:

关于您的问题,其中某些点在面板的边缘处被切除,您可以通过将图转换为grob来解决该问题.关闭面板的剪辑:

As for your issue with some points being cut off at the edge of the panel, you can get around it by converting the plot to a grob, & turning off the panel's clipping:

# plot with restricted range
p <- ggplot(data = mydata, aes(x = x, y = y)) +
  geom_point(shape = 21, size = 5, color = "black", fill = "orange", stroke = 1) +
  scale_x_continuous(name = 'Number of Contacts') +
  scale_y_continuous(name = "Remembered (%)", 
                     expand = c(0, 0),
                     sec.axis = sec_axis(trans = ~ (.-min(.))*100/(max(.)-min(.)),
                                         name = "Remembered (Index)",
                                         breaks = break2)) +
  coord_cartesian(ylim = c(20, 73)) +
  theme(plot.margin = margin(t = 10, unit = "pt")) # increase top margin as some points
                                                   # are very close to the top edge

# convert to grob
gp <- ggplotGrob(p)

grid::grid.draw(gp) # verify that the points at the edge are cut off

# turn off clipping for panel
gp$layout$clip[gp$layout$name=="panel"] <- "off"

grid::grid.draw(gp) # points at the edge are now visible

这篇关于我可以精确限制ggplot轴范围吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆