x轴离散时的geom_ribbon覆盖 [英] geom_ribbon overlay when x-axis is discrete

查看:129
本文介绍了x轴离散时的geom_ribbon覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一组箱形图上放置一个 underlay ,以指示从单独的数据源和单独的数据框中提取的某些数据的范围.制作覆盖很容易;制作底衬很困难.

I'd like to put an underlay on a set of boxplots to indicate the range of some data drawn from a separate source, and in a separate data frame. Making an overlay is easy; making an underlay is difficult.

想象一下,出于这些目的,我正在尝试将mtcars中的某些数据添加到diamonds数据的箱线图中:

Imagine, for these purposes, that I am trying to underlay some data from mtcars onto a boxplot of diamonds data:

# 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))

这很好用-除了我(实际上是我的合著者)希望将geom_ribbon放在之下而不是之上. (据称,色带的颜色会干扰箱线图的颜色).当我尝试像这样反转几何图形的顺序时:

This works fine - except that I (actually: my coauthors) would like to have the geom_ribbon lie under rather than over the boxplots. (The color of the ribbon allegedly interferes with the color of the boxplot). When I try to reverse the order of the geoms like so:

ggplot() + 
  geom_ribbon(data=mtcarsQ, aes(x=x, ymin=ymin, ymax=ymax), alpha=0.2)+
  geom_boxplot(data=diamonds, aes(x=cut, y=y))

我得到Error: Discrete value supplied to continuous scale,因为ggplot不想将因子映射到连续尺度.

I get Error: Discrete value supplied to continuous scale, because ggplot doesn't want to map a factor to a continuous scale.

我可以尝试使因子成为数字:

I could try to make the factor numeric:

ggplot() + 
  geom_ribbon(data=mtcarsQ, aes(x=x, ymin=ymin, ymax=ymax), alpha=0.2)+
  geom_boxplot(data=diamonds, aes(x=as.numeric(cut), y=y))

但是,这会更改x轴标签.我本以为可以将轴标签添加回如下:

However, this changes the x axis labels. I would have thought that I could add the axis labels back as follows:

ggplot() + 
  geom_ribbon(data=mtcarsQ, aes(x=x, ymin=ymin, ymax=ymax), alpha=0.2)+
  geom_boxplot(data=diamonds, aes(x=as.numeric(cut), y=y, fill=cut, group=cut)) +
  scale_x_discrete(labels=levels(diamonds$cut)) +
  coord_cartesian(ylim=c(0, 12))

但是由于我不了解的原因,x轴出现了额外的因子水平.

But the x-axis comes out with extra factor levels for reasons I don't understand.

当箱形图具有离散的x轴时,是否有更简单的方法在箱形图下方放置一个矩形?

Is there a simpler way to put a rectangle underneath a boxplot when the boxplot has a discrete x axis?

推荐答案

强制比例尺默认设置为默认值的最通用方法是从geom_blank开始,它将定义比例尺但不绘制任何内容.我相信它是专门为这种情况而设计的:

The most general way to force scales to default the way you want them to is to starting with geom_blank, which will define the scales but draw nothing. I believe it is designed for exactly this situation:

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))

这篇关于x轴离散时的geom_ribbon覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆