如何在需要反转电平的R中将误差线添加到分组的堆叠Barplot中? [英] How to add error-bars to a grouped stacked barplot in R which requires reversing of levels?
问题描述
我正在尝试创建带有错误条的堆叠条形图. 数据文件在此处: https://github.com/Ginko-Mitten/Experimental.git
I'm trying to create a stacked barplot with errorbars. The datafile is here: https://github.com/Ginko-Mitten/Experimental.git
library(ggplot2)
Dat1<-Methods_Recovery_Comparison
Volunteer<-as.factor(Dat1$Volunteer)
Method<-as.factor(Dat1$Method)
Phase<-as.factor(Dat1$Phase)
Recovery<-as.numeric(Dat1$Recovery)
SD<-as.numeric(Dat1$SD)
SDU<-Recovery+SD
SDL<-Recovery-SD
Ext<-data.frame(Volunteer,Method,Phase,Recovery,SD,SDU,SDL)
ggplot(Ext,aes(Method))+
geom_bar(aes(weight = Recovery, fill = Phase))+
facet_grid(~Volunteer)+
geom_errorbar(aes(ymax=Recovery+SD,ymin=Recovery-SD))
我真的很想:
1.要反转的相位顺序
2.误差线与误差线正确对齐
I would really like:
1. The order of the Phase to be reversed
2. The error-bars to align properly to the bars
附加要求: 是否可以针对志愿者3->方法2->阶段3"的值专门减轻或使条空心化? (相应的恢复值为10.5).该特定值是一个异常值,并希望在撰写时在说明中突出显示.
Additional Request: Would it be possible to specifically lighten or make the bar hollow for value of Volunteer 3-> Method 2 -> Phase 3? (The corresponding recovery value is 10.5). That particular value was an anomalous one and would like to highlight it in the description while writing up.
推荐答案
ggplot2
的创建者并不喜欢堆积条形图上的错误条(请参阅:
The creator of ggplot2
is not a big fan of error bars on stacked barchart (see: https://github.com/tidyverse/ggplot2/issues/1079) so there is no convenient way implemented in ggplot2
to do that.
诀窍是计算错误条在堆栈中的y位置.在这里,我使用dplyr
在Volunteer和Method函数中创建了新列,该列将累加添加Recovery以获取每个误差线的y位置:
The trick is to calculate the y position of the error bar in the stack. Here I used dplyr
to create new column in function of Volunteer and Method that will cumulative add Recovery in order to get the y position for each error bar:
library(dplyr)
DF <- df %>% group_by(Volunteer, Method) %>%
mutate(Phase = factor(Phase, levels = c("Part3","Part2","Part1"))) %>%
mutate(SDpos = cumsum(Recovery))
# A tibble: 27 x 6
# Groups: Volunteer, Method [9]
Volunteer Method Phase Recovery SD SDpos
<chr> <chr> <fct> <dbl> <dbl> <dbl>
1 P1 Method1 Part1 50.6 0.1 50.6
2 P1 Method1 Part2 15.4 1.2 66
3 P1 Method1 Part3 8.8 2.1 74.8
4 P1 Method2 Part1 50.6 0.1 50.6
5 P1 Method2 Part2 15.4 1.2 66
6 P1 Method2 Part3 14.6 2.9 80.6
7 P1 Method3 Part1 50.6 0.1 50.6
8 P1 Method3 Part2 14.6 1.9 65.2
9 P1 Method3 Part3 16.6 2.3 81.8
10 P2 Method1 Part1 25.8 0.1 25.8
# … with 17 more rows
然后,我将制作堆叠的条形图,并根据我创建的SDpos列添加geom_errorbar
(这将是我的y值):
Then, I will make the stacked bargraph and add geom_errorbar
based on the SDpos column I made (it will be my y values):
library(ggplot2)
ggplot(DF, aes(x = Method, y = Recovery, fill = Phase))+
geom_col()+
facet_wrap(.~Volunteer) +
scale_fill_discrete(breaks = c("Part1","Part2","Part3"))+
geom_errorbar(aes(ymin = SDpos-SD, ymax = SDpos+SD), width = 0.2)
它看起来像你想要得到的吗?
Does it look what you are trying to get ?
这篇关于如何在需要反转电平的R中将误差线添加到分组的堆叠Barplot中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!