相关疑难解决方法(0)

当x轴是离散的时,geom_ribbon覆盖

我想在一组箱图上加一个底层来表示从一个单独的数据源和一个单独的数据框中提取的一些数据的范围.制作的叠加是容易的; 制作衬底很难.

想象一下,出于这些目的,我试图将一些数据从mtcars一个diamonds数据箱图中得到:

# Find quantiles of mtcars$drat
mQ <- quantile(mtcars$wt, c(0.025, 0.975))

# Place them in a data frame with factor limits of diamonds$cut
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))

# Plot diamonds$cut, with overlay of quantiles from mtcars
ggplot() + 
  geom_boxplot(data=diamonds, aes(x=cut, y=y)) +
  geom_ribbon(data=mtcarsQ, aes(x=x, ymin=ymin, ymax=ymax), alpha=0.2)+
  coord_cartesian(ylim=c(0, 12))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这很好 - 除了我(实际上:我的共同作者)想要在geom_ribbon谎言下面而不是箱形图上.(据称色带的颜色会干扰盒子图的颜色).当我试图颠倒geoms的顺序时:

ggplot() + 
  geom_ribbon(data=mtcarsQ, aes(x=x, ymin=ymin, ymax=ymax), alpha=0.2)+
  geom_boxplot(data=diamonds, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

4
推荐指数
1
解决办法
3457
查看次数

标签 统计

ggplot2 ×1

r ×1