在ggplot2中仅填充(扩展)连续比例的顶部

时间:2015-12-17 19:08:47

标签: r ggplot2

我有一些共享x轴但有两个不同y变量的数据:

set.seed(42)
data = data.frame(
    x = rep(2000:2004, 2),
    y = c(rnorm(5, 20, 5), rnorm(5, 150, 15)),
    var = rep(c("A", "B"), each = 5)
)

我使用分面线图来显示数据:

p = ggplot(data, aes(x, y)) +
    geom_line() +
    facet_grid(var ~ ., scales = "free_y")

enter image description here

我希望y轴包含0.这很容易:

p + expand_limits(y = 0)

然后我的数据看起来过于接近我的方面。所以我想填补轴的范围。通常scale_y_continuous(expand = ...)用于填充轴,但填充对称地应用于顶部和底部,使y轴顺利低于 0。

p + expand_limits(y = 0) + 
    scale_y_continuous(expand = c(0.3, 0.2))
# the order of expand_limits and scale_y_continuous
# does not change the output

enter image description here

由于具有自由y刻度的刻面,我无法明确设置限制。 什么是将y-scale延伸到0(不低于!)的最佳方法,同时在y标度的顶部乘法填充?

1 个答案:

答案 0 :(得分:3)

您可以为每个构面创建一个带有单个点的额外数据集,并使用Meteor.loginWithPassword(badUid, badPassword)无形地绘制它。该点被选择为大于给定方面中的最大值的固定因子。在这里,我选择该因子为1.5以使效果清晰可见:

geom_blank()

这就是我得到的:

enter image description here