在x方向上扩展ggplot`geom_ribbon()

https://stackoverflow.com/questions/55290819/extend-ggplot-geom-ribbon-in-the-x-directionlibrary(tidyverse)mQ <- quantile(mtcars$wt, c(0.025, 0.975))mtcarsQ <- data.frame(x = c(min(as.numeric(diamonds$cut)),                             max(as.numeric(diamonds$cut))),                       ymin = rep(mQ[1], 2),                       ymax = rep(mQ[2], 2))ggplot() +   geom_blank(data = diamonds, aes(x = cut, y = y)) +  geom_ribbon(data = mtcarsQ, aes(x = x, ymin = ymin, ymax = ymax), alpha=0.2) +  geom_boxplot(data = diamonds, aes(x = cut, y = y, fill = cut, group = cut)) +  coord_cartesian(ylim = c(0, 12)) +   theme_bw()

我想将我geom_ribbon()的代码从以上代码块扩展到x轴上的任一方向。类似于下面的图片。我的首选方法geom_ribbon()是完全融入蓝色虚线框。

我该怎么做呢?

您可以从中减去/加0.5 mtcarsQ$x

mtcarsQ <- data.frame(x = c(min(as.numeric(diamonds$cut)) - 0.5,                            max(as.numeric(diamonds$cut)) + 0.5),                      ymin = rep(mQ[1], 2),                      ymax = rep(mQ[2], 2))ggplot() +  geom_blank(data = diamonds, aes(x = cut, y = y)) +  geom_ribbon(data = mtcarsQ, aes(x = x, ymin = ymin, ymax = ymax), alpha=0.2) +  geom_boxplot(data = diamonds, aes(x = cut, y = y, fill = cut, group = cut)) +  coord_cartesian(ylim = c(0, 12)) +  theme_bw()
(0)

相关推荐