分享

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

 昵称27943849 2020-11-20
https:///questions/55290819/extend-ggplot-geom-ribbon-in-the-x-direction

library(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()是完全融入蓝色虚线框。

我该怎么做呢?

扩展的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

    0条评论

    发表

    请遵守用户 评论公约