【问题标题】:Combining 2 plots of different data (boxplot and points)结合 2 个不同数据的图(箱线图和点)
【发布时间】:2015-12-30 08:49:32
【问题描述】:

我想在彼此的顶部绘制两个图表like in this post

实验数据:我有一个连续变量,在名为expt$iso_xs[,8] 的列表中显示给定日期的风的角度,然后我在expt$iso_xs[,2] 中有对应于该角度的风速。

df<-data.frame(expt$iso.xs)

head(expt$iso.xs)
         [,1]     [,2]     [,3]      [,4]      [,5] [,6] [,7]   [,8]
 736105.4 16.62729 2.183740  7.234774 0.9791632 4.01 4.20 238.62
 736105.4 18.96705 2.489668  7.036234 0.9640366 3.82 4.00 243.14
 736105.5 20.52089 2.687636 10.355394 1.3698454 4.99 5.14 247.02
 736105.5 19.94449 2.611556 10.306912 1.3655301 4.85 5.12 249.57
 736105.5 19.43309 2.551787 11.098302 1.4646251 4.83 5.12 243.89
 736105.5 20.48259 2.689075 11.928011 1.5710530 4.89 5.09 254.23

看起来像这样:

模拟数据:我有一个 data.frame z,其中包含对上述角度子集 (0-90º) 的预测。

head(z,15)
   Tracer angle treatment bigangle
  71.101     0         S      150
  71.101     0         S      150
  71.105     15         S      165
  71.105     15         S      165
  71.098     30         S      180
  71.098     45         S      195
  71.114     60         S      210
  71.114     80         S      230
  71.110     90         S      240

使用 bigangle 作为因子,Tracer 作为:

ggplot() +
  geom_boxplot(data=z, aes(y = (3600/Tracer/93.241), x = factor(bigangle)),outlier.shape = NA,outlier.colour = NA)+
  coord_cartesian(ylim=c(0, 1))+
  labs(x = "Angle", y = "Normalised ACh" )+
  scale_x_discrete(labels=seq(0,360,10))+
  theme_classic()

看起来像这样:

我想将箱线图叠加在红点部分(150º 和 240º 之间)的顶部,但以下方法不起作用:

ggplot() +
      geom_boxplot(data=z, aes(y = (3600/Tracer/93.241), x = factor(bigangle)),outlier.shape = NA,outlier.colour = NA)+
      geom_point(data=df, aes(y = X2/45, x = X8),color="red")+
      coord_cartesian(ylim=c(0, 1))+
      labs(x = "Angle", y = "Normalised ACh" )+
      scale_x_discrete(labels=seq(0,360,10))+
      theme_classic()

任何想法将不胜感激, 干杯

【问题讨论】:

  • 请注意,这是与stackoverflow.com/posts/34515961/edit 不同的问题,但使用相同的数据
  • 您能否用可复制粘贴的数据制作一个可重现的最小示例?使用dput() 共享数据、使用内置数据或模拟数据(使用随机种子)。我不认为expt 的6 行和z 的15 行足以演示代码。

标签: r plot ggplot2


【解决方案1】:

我认为您唯一的问题是尝试为连续数据指定离散的 x 比例。那你需要一个 group 来为你的 boxplot 几何图形。

作为一个说明性的例子:

mt = mtcars
mt$wt_bin = cut(mt$wt, breaks = c(1, 3, 4.5, 6))
ggplot(mt, aes(x = wt, y = mpg)) +
    geom_point() +
    geom_boxplot(aes(group = wt_bin, x = wt), alpha = 0.4)

正如geom_boxplot 帮助所说:

您也可以使用带有连续 x 的箱线图,只要您提供 一个分组变量。 cut_width 特别好用

帮助中的例子显示了这段代码:

ggplot(diamonds, aes(carat, price)) +
  geom_boxplot(aes(group = cut_width(carat, 0.25)))

当然,您可以添加一个geom_point 层(尽管在diamonds 数据中,点太多,因此无法成为一个漂亮的图)。

对于您的比例,除非您在轴上有因子,否则不要使用离散比例。你可能想要scale_x_continuous(breaks = seq(0, 360, 10))

可以通过data 参数以通常的方式使用不同的数据集。继续前面的示例,但对geom_point 层使用不同的数据:

similar_to_mt = data.frame(wt = runif(100, 1, 6), mpg = rnorm(100, 20, 4))
ggplot(mt, aes(x = wt, y = mpg)) +
    geom_point(data = similar_to_mt) +
    geom_boxplot(data = mt, aes(group = wt_bin, x = wt), alpha = 0.4)

【讨论】:

  • 谢谢格雷戈尔。例如,您能否将钻石数据叠加到 mt 图上?这就是我正在尝试的......
  • 下次您提出问题时,请务必重复分享数据。这样可以更快地回答。
  • 太棒了!非常感谢格雷戈尔,我真的很感激。我正在尝试学习如何让数据代表我自己的下次。只是出于好奇, scale_x_continuous(breaks = seq(0, 360, 10)) 参数给出了一个错误消息:错误:提供给连续比例的离散值。你觉得对吗?
  • 由于您没有共享数据,因此您的工作是转换您的数据以匹配我模拟的数据。在我的示例数据中,我总是将x 美学映射到numeric 类的列。听起来就像在您的代码中将x 映射到一列类因子。不要那样做。将其转换为numeric
  • 将其转换为numeric(使用as.numeric(as.factor()),正如我们所说的in this question。我在示例中使用的唯一因素是我映射到group的列,它决定了哪些点进入哪个箱线图。
猜你喜欢
  • 2016-10-30
  • 1970-01-01
  • 2020-07-23
  • 2023-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
相关资源
最近更新 更多