[英]Group-specified geom_boxplot from summary statistics fails to generate boxplots in r
I am trying to make a box plot from summary statistics generated from simulation data--I am not plotting raw data.我正在尝试根据模拟数据生成的汇总统计数据制作箱线图——我没有绘制原始数据。 The summary data look like this:汇总数据如下所示:
require(tidyverse)
df <- tibble(Source = c("A","A","B","B","C","C"),
Group = c("y","n","y","n","y","n"),
y2.5 = c(0.592,0.471,0.182,0.285,0.024,0.031),
y25 = c(0.633,0.53,0.217,0.325,0.059,0.081),
y50 = c(0.673,0.547,0.24,0.347,0.08,0.106),
y75 = c(0.699,0.58,0.267,0.370,0.103,0.130),
y97.5 = c(0.764,0.61,0.312,0.414,0.146,0.173))
I want to generate box plots for each Source
along the x-axis, paired by Group
.我想为每个Source
沿 x 轴生成箱线图,由Group
配对。 As you can see, there is one set of summary values for each combination of group and source.如您所见,组和源的每个组合都有一组汇总值。
I see from the vignettes that geom_boxplot(aes())
accepts summary values plotted this way:我从小插图中看到geom_boxplot(aes())
接受以这种方式绘制的汇总值:
ggplot(df, aes(Source, group = Group))+
geom_boxplot(aes(ymin = y2.5, lower = y25, middle = y50, upper = y75, ymax = y97.5),
stat = "identity")
But this code run on these data generates this error:但是在这些数据上运行的这段代码会产生这个错误:
Error: Can't draw more than one boxplot per group. Did you forget aes(group = ...)?
I have moved the group argument into the box plot aes()
call but this produces the same error message.我已将 group 参数移到箱线图aes()
调用中,但这会产生相同的错误消息。 This feels a lot like a simple problem caused by some late-night oversight but I just can't see it... maybe I have been staring too long.这感觉很像深夜疏忽导致的一个简单问题,但我就是看不到它……也许我盯着看得太久了。 Any help with this would be much appreciated.对此的任何帮助将不胜感激。
I don't think you can map to "group" (more details: https://ggplot2.tidyverse.org/reference/aes_group_order.html ), as far as I know you need to map to an aesthetic like "color", "fill", "alpha", "shape", "size", and/or "linetype" to get the outcome you want, eg我不认为你可以映射到“组”(更多细节: https : //ggplot2.tidyverse.org/reference/aes_group_order.html ),据我所知你需要映射到像“颜色”这样的美学, “填充”、“阿尔法”、“形状”、“大小”和/或“线型”以获得您想要的结果,例如
library(tidyverse)
df <- tibble(Source = c("A","A","B","B","C","C"),
Group = c("y","n","y","n","y","n"),
y2.5 = c(0.592,0.471,0.182,0.285,0.024,0.031),
y25 = c(0.633,0.53,0.217,0.325,0.059,0.081),
y50 = c(0.673,0.547,0.24,0.347,0.08,0.106),
y75 = c(0.699,0.58,0.267,0.370,0.103,0.130),
y97.5 = c(0.764,0.61,0.312,0.414,0.146,0.173))
ggplot(df, aes(x = Source, fill = Group)) +
geom_boxplot(aes(ymin = y2.5, lower = y25,
middle = y50, upper = y75,
ymax = y97.5),
stat = "identity")
ggplot(df, aes(x = Source, color = Group)) +
geom_boxplot(aes(ymin = y2.5, lower = y25,
middle = y50, upper = y75,
ymax = y97.5),
stat = "identity")
Or, another potential alternative is to plot the interaction between Group and Source, eg或者,另一种可能的替代方法是绘制 Group 和 Source 之间的交互,例如
ggplot(df, aes(x = interaction(Group, Source))) +
geom_boxplot(aes(ymin = y2.5, lower = y25,
middle = y50, upper = y75,
ymax = y97.5),
stat = "identity")
Created on 2021-07-12 by the reprex package (v2.0.0)由reprex 包( v2.0.0 ) 于 2021 年 7 月 12 日创建
Each group label needs to be distinct.每个组标签都需要不同。 You can use an interaction with source to force that您可以使用与源的交互来强制
ggplot(df, aes(Source))+
geom_boxplot(aes(ymin = y2.5, lower = y25, middle = y50, upper = y75, ymax = y97.5,
group = interaction(Source, Group)),
stat = "identity")
But using fill as suggested by jared_mamrot is probably a better solution.但是按照 jared_mamrot 的建议使用 fill 可能是更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.