我试图在R中为两个不同的组创建一个变量的均值和sd(并排)的图,以得到这样的东西。其中蓝条是平均值,橙条是标准差。
我用 …
它似乎 position="dodge" 适用于相同x的geom,但不适用于stat。我想出了两个解决方案。
position="dodge"
在第一个,我保留你的stat_summary和使用 position_nudge 手动将条形图放在指定的位置。请注意图例如何不起作用,因为没有实际的绘图数据,只有stat图层。
position_nudge
在第二部分中,我在ggplot之前进行了数据分析,使用group_by,汇总,然后收集以使数据变长。然后我们可以使用常规 geom_col 现在数据已经处理完毕。
geom_col
library(tidyverse) tibble(interviewer = c("i2", "i1", "i1", "i2", "i1"), tTTO = c(245, 251, 99, 85, 101)) %>% ggplot(aes(x=interviewer, y=tTTO)) + theme_light() + labs(title = "Figure 3. Time taken to complete a single TTO task, by interviewer", x=NULL, y=NULL) + theme(plot.title = element_text(face = "bold"), legend.position = "bottom") + geom_bar(stat = "summary", fun.y = "mean", position = position_nudge(x = -0.125, y = 0), width = 0.25, fill = "blue") + geom_bar(stat = "summary", fun.y = "sd", position = position_nudge(x = 0.125, y = 0), width = 0.25, fill = "orange")
# Notice that the legend does not work for stat geoms tibble(interviewer = c("i2", "i1", "i1", "i2", "i1"), tTTO = c(245, 251, 99, 85, 101)) %>% group_by(interviewer) %>% summarize(mean(tTTO), sd(tTTO)) %>% gather(key = "type", value = "value", 2:3) %>% ggplot(aes(x=interviewer, y=value, fill=type)) + theme_light() + labs(title = "Figure 3. Time taken to complete a single TTO task, by interviewer", x=NULL, y=NULL) + theme(plot.title = element_text(face = "bold"), legend.position = "bottom") + geom_col(position = "dodge", width = 0.25) + scale_fill_manual(values = c("blue","orange"))
创建于2019-03-04由 代表包 (v0.2.1)