简体   繁体   中英

ggplot error bar positioning

I hope I've formatted the question correctly and have searched far and wide for a similar question so my apologies if this is a duplicate

When I put error bars on a grouped bar chart it ggplot simply puts the error bars between the grouped bars like in the image below in rplot however I want the error bars to be in the middle of their respect bars

rplot

the data looks like this

print(dfm)
  REEF           variable    value Errortype     Error
1 Reef 1 Machine.Percentage 23.35068        ME 0.1341473
2 Reef 2 Machine.Percentage 23.85531        ME 0.4876110
3 Reef 3 Machine.Percentage 18.36640        ME 0.6022585
4 Reef 4 Machine.Percentage 16.98787        ME 0.5596818
5 Reef 1   Human.Percentage 21.12382        HE 0.1620290
6 Reef 2   Human.Percentage 28.22039        HE 0.1732592
7 Reef 3   Human.Percentage 18.14550        HE 0.8022002
8 Reef 4   Human.Percentage 15.50208        HE 0.4999109

otherwise the same data but using dput if that is preferable

> dput(dfm)
structure(list(X = 1:8, REEF = structure(c(1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L), .Label = c("Reef 1", "Reef 2", "Reef 3", "Reef 4"
), class = "factor"), variable = structure(c(2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L), .Label = c("Human.Percentage", "Machine.Percentage"
), class = "factor"), value = c(23.35068462, 23.85531136, 18.36640212, 
16.98786965, 21.12382394, 28.22039072, 18.14550265, 15.50208154
), Errortype = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("HE", 
"ME"), class = "factor"), Error = c(0.134147251, 0.487611042, 
0.602258513, 0.559681767, 0.162029047, 0.173259241, 0.802200189, 
0.499910856)), .Names = c("X", "REEF", "variable", "value", "Errortype", 
"Error"), class = "data.frame", row.names = c(NA, -8L))

And this is the code

 ggplot (data = dfm, aes(x = REEF, y = value))+ 
        geom_bar(aes(fill = variable),stat = "identity",position = "dodge", width = 0.9)+
        theme_bw()+scale_fill_brewer(palette="Set1")+
        theme(axis.text.x = element_text(angle = 45, hjust = 1))+
        ylab("% Hard Coral")+xlab("Reef Name")+theme(legend.title=element_blank())+
        ggtitle("Hard Coral group_KER 1")+
        geom_errorbar(aes(ymin=value-Error, ymax=value+Error),
                      size=.5,  
                      width=.2,
                      position=position_dodge(0.9))

Will be incredibly grateful for any insights

Need to match how the errorbar geom is grouped with the bars:

ggplot (data = dfm, aes(x = REEF, y = value))+ 
        geom_bar(aes(fill = variable),stat = "identity",position = "dodge", width = 0.9)+
        theme_bw()+scale_fill_brewer(palette="Set1")+
        theme(axis.text.x = element_text(angle = 45, hjust = 1))+
        ylab("% Hard Coral")+xlab("Reef Name")+theme(legend.title=element_blank())+
        ggtitle("Hard Coral group_KER 1")+
        geom_errorbar(aes(ymin=value-Error, ymax=value+Error, group = variable),
                      size=.5,
                      width=.2,
                      position=position_dodge(0.9))

The data in your ggplot call is grouped together by the variable field only in the call aes(fill = variable) . As such, only geom_bar knows about that grouping.

Two solutions present themselves: first, you could inform the geom_errorbar of the grouping by adding group = variable so that the aes call becomes aes(ymin = value-Error, ymax = value+Error, fill = variable) .

Alternatively, you could move the fill = variable from the geom_bar aesthetic into the ggplot aesthetic so that the grouping is available to all subsequent functions: aes(x = REEF, y = value, fill = variable) .

Incidentally, using geom_bar with stat = "identity" can, and should, be achieved by using geom_col which subsumes it. As such, you can obtain your desired output by using the command:

ggplot (data = dfm, aes(x = REEF, y = value, fill = variable))+ 
  geom_col(position = "dodge", width = 0.9)+
  theme_bw()+scale_fill_brewer(palette="Set1")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  ylab("% Hard Coral")+xlab("Reef Name")+theme(legend.title=element_blank())+
  ggtitle("Hard Coral group_KER 1")+
  geom_errorbar(aes(ymin=value-Error, ymax=value+Error),
                size=.5,  
                width=.2,
                position=position_dodge(0.9))

which gives the output: 用校正后的误差线绘图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM