首页 > 解决方案 > 在 R Studio 中创建分组箱线图的问题

问题描述

我正在尝试使用 ggplot2 在 R Studio 中创建分组箱线图。

我的数据如下所示:

数据集

(总共 4 个不同的 APP.mm 值)

还有我的数据集样本dput()

structure(list(APP.mm = c(408.5, 408.5, 408.5, 408.5, 408.5, 
408.5, 408.5, 408.5, 408.5, 408.5, 408.5, 237.5, 237.5, 237.5, 
237.5, 237.5, 237.5, 237.5, 237.5, 237.5, 237.5, 237.5, 237.5, 
237.5, 237.5, 132.1, 132.1, 132.1, 132.1, 132.1, 132.1, 132.1, 
274, 274, 274, 274, 274, 274, 274, 274, 274), Weight_mg = c(112, 
106.72, 118.4, 62.3, 86.2, 70.3, 56.5, 71, 61.4, 61.7, 62.7, 
81.1, 125.5, 71, 120.4, 75.3, 68.1, 95.9, 77.5, 95.5, 85.3, 64.1, 
27.6, 20.5, 77.9, 86.3, 120, 100.2, 86.4, 67.8, 104.4, 90.1, 
71.1, 89, 77.4, 77.1, 60.5, 78.7, 87.9, 71.1, 76.5), Germination = c(0L, 
1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 
1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 
1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L)), .Names = c("APP.mm", "Weight_mg", 
"Germination"), row.names = c(6L, 7L, 8L, 9L, 88L, 89L, 90L, 
91L, 92L, 93L, 94L, 2111L, 2112L, 2113L, 2114L, 2115L, 2116L, 
2117L, 2118L, 2119L, 2120L, 2121L, 2122L, 2123L, 2124L, 2674L, 
2675L, 2676L, 2677L, 2678L, 2679L, 2680L, 3193L, 3194L, 3195L, 
3196L, 3197L, 3198L, 3199L, 3200L, 3201L), class = "data.frame")

我现在正在尝试创建一个分组箱线图,其中 APP.mm 在 x 轴上,重量在 Y 轴上,并且由 APP.mm 分组到发芽成功(1)或没有发芽成功(0)的值

我为 R 尝试了这些代码:

ggplot(data = gallsdtg, aes(x=APP.mm, y=Weight_mg)) +
  geom_boxplot(aes(fill=Germination))

Warning message:
Continuous x aesthetic -- did you forget aes(group=...)? 


ggplot(data = gallsdtg, aes(x=APP.mm, y=Weight_mg)) +
 geom_boxplot(aes(group=Germination))

qplot(gallsdtg$APP.mm, gallsdtg$Weight_mg,
 fill=factor(galls$Germination), geom="boxplot")

导致这个情节: 情节 1

我只是从不同的位置在相同的数据结构上尝试了完全相同的代码,并得到了我想要的情节。

qplot(germination2017extras$APP.mm, germination2017extras$Weight_mg, 
  fill=factor(germination2017extras$Germination), geom="boxplot", 
  xlab="mean APP in mm", ylab="Weight in mg", ylim=c(0,200))

情节 2

有人可以告诉我我在这里做错了什么吗?太感谢了!

标签: rggplot2rstudioboxplot

解决方案


推荐阅读