如何在ggplot2中动态调整y轴范围?

3
如何调整 y 轴,使得注释在这种类型的图表中也能正常显示?
library(ggplot2)

ggplot(diamonds, aes(x = cut, y = depth)) +
  facet_wrap(~ color) +
  stat_summary(fun.y = sum, geom="bar", fill = "yellow", aes(label=cut, vjust = 0)) +
  stat_summary(fun.y = sum, geom="text", aes(label=cut), vjust = 0)

enter image description here

例如,在facet G中,“Ideal”标注未正确显示。Y轴范围应该动态计算,以便在柱形图上方始终有一些空间用于标注。因此,我不能使用固定的y轴范围。

1个回答

6
在您现有的代码之后使用+ scale_y_continuous(expand=c(0.15,0))如何?expand=需要一个由乘法和加法比例因子组成的2元数列。请参考以下图片:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接