Home > Enterprise >  Why doesn't the error bar show up when using ggplot?
Why doesn't the error bar show up when using ggplot?

Time:09-18

The error bar doesn show up when plotting.

Here is my original code:

aa<-structure(list(sample = c("L-FF", "L-FF", "L-FF", "L-FF", "L-FF", 
"L-FF", "L-SS", "L-SS", "L-SS", "L-SS", "L-SS", "L-SS", "L-MM", 
"L-MM", "L-MM", "L-MM", "L-MM", "L-MM", "L-MF", "L-MF", "L-MF", 
"L-MF", "L-MF", "L-MF", "L-MS", "L-MS", "L-MS", "L-MS", "L-MS", 
"L-MS"), group = c("FF", "FF", "FF", "FF", "FF", "FF", "SS", 
"SS", "SS", "SS", "SS", "SS", "MM", "MM", "MM", "MM", "MM", "MM", 
"MF", "MF", "MF", "MF", "MF", "MF", "MS", "MS", "MS", "MS", "MS", 
"MS"), group2 = c("P0N0", "P0N1", "P1N0", "P1N1", "P2N2", "P3N0", 
"P0N0", "P0N1", "P1N0", "P1N1", "P2N2", "P3N0", "P0N0", "P0N1", 
"P1N0", "P1N1", "P2N2", "P3N0", "P0N0", "P0N1", "P1N0", "P1N1", 
"P2N2", "P3N0", "P0N0", "P0N1", "P1N0", "P1N1", "P2N2", "P3N0"
), depth = c("Low", "Low", "Low", "Low", "Low", "Low", "Low", 
"Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", 
"Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", 
"Low", "Low", "Low", "Low", "Low"), mean = c(8.43666666666667, 
8.41666666666667, 8.42333333333333, 8.50333333333333, 8.34, 8.37, 
8.19333333333333, 8.32, 8.34333333333333, 8.39, 8.25666666666667, 
8.48, 8.34666666666667, 8.24666666666667, 8.31, 8.39333333333333, 
8.24, 8.49333333333333, 8.35666666666667, 8.35, 8.51333333333333, 
8.48, 8.34333333333333, 8.52666666666667, 8.34333333333333, 8.20666666666667, 
8.30666666666667, 8.4, 8.12666666666667, 8.47666666666667), se = c(0.0696020433927371, 
0.0926162932629988, 0.0829323686974982, 0.0545690184791495, 0.0435889894354068, 
0.0802080627701062, 0.202182535788277, 0.0550757054728609, 0.0744610263456291, 
0.0529150262212925, 0.0584047182264505, 0.0458257569495582, 0.0669161996662826, 
0.0811035004039766, 0.0702376916856848, 0.061734197258174, 0.056862407030773, 
0.0523874454850056, 0.0545690184791497, 0.138924439894498, 0.0120185042515464, 
0.0568624070307733, 0.0392994204085055, 0.0762306441735284, 0.066916199666282, 
0.170522074165715, 0.12387269450708, 0.0360555127546394, 0.0484194634877803, 
0.0463081466314994)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -30L))

And here is my code:

library(ggplot2)
library(ggpubr)
library(readxl)
library(ggsci)

p<-ggplot(aa, aes(group,mean,fill=group2)) 
  geom_bar(stat="identity",width = 0.7,size=0.2,alpha=1,color='black',position="stack") 
  geom_errorbar(aes(ymin =mean-se, ymax =mean   se),width=0.2,position="identity")  
  theme(legend.text=element_text(family="Times",size=10)) 
  labs(x ='' , y = 'Organic matter (mg/kg)', fill = 'sample')  
  theme_bw() 
  scale_y_continuous(expand = c(0, 0), limit = c(0,60),breaks = seq(0,60,10)) 
  guides(fill=guide_legend(title = '')) 
  theme(panel.grid = element_blank(),  panel.background = element_blank()) 
  theme(
    text = element_text(family="Times",size=14), 
    plot.title = element_text(family="Times",size = 14),
    axis.text.x = element_text(family="Times",size = 14, colour= 'black'),
    axis.text.y = element_text(family="Times",size = 14, colour= 'black'),
    legend.text = element_text(family="Times",size = 14, colour= 'black')) 
  theme(axis.ticks.length = unit(0.1,'cm'),
        plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm")) 
p   scale_fill_nejm()

Here is the figre I got with the code: enter image description here But the error bar doesn't show up. Here is the effect I expected: enter image description here

How strange can it be? enter image description here

Any suggestions are welcome! Thank you advance!

CodePudding user response:

There are two problems here. The first is that your standard errors are very small compared to the bars themselves. The size of each standard error ranges from 0.012 to 0.20, meaning your error bar height varies from 0.024 to 0.40, but the y axis ranges from 0 to 60. That means that your standard errors vary between 1/2500 and 1/150 the height of your panel. Even with a relatively large panel, this means the error bar will be between 1 and 3 pixels tall, effectively appearing as a flat line. The effect can be improved a little by dodging instead of stacking, since you can effectively make everything 6 times taller, but the error bars remain very small:

ggplot(aa, aes(group, mean, fill = group2)) 
  geom_col(width = 0.7, size = 0.2, alpha = 1, color = 'black', 
           position = position_dodge(width = 0.8))  
  geom_errorbar(aes(ymin =mean-se, ymax =mean   se, group = group2),
                width = 0.2, position = position_dodge(width = 0.8))  
  theme(legend.text=element_text(family="Times",size=10)) 
  labs(x ='' , y = 'Organic matter (mg/kg)', fill = 'sample')  
  theme_bw() 
  scale_y_continuous(expand = c(0, 0), limit = c(0,10),breaks = seq(0,60,10)) 
  guides(fill=guide_legend(title = '')) 
  theme(panel.grid = element_blank(),  panel.background = element_blank()) 
  theme(
    text = element_text(family="Times",size=14), 
    plot.title = element_text(family="Times",size = 14),
    axis.text.x = element_text(family="Times",size = 14, colour= 'black'),
    axis.text.y = element_text(family="Times",size = 14, colour= 'black'),
    legend.text = element_text(family="Times",size = 14, colour= 'black')) 
  theme(axis.ticks.length = unit(0.1,'cm'),
        plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"))   
  scale_fill_nejm()

enter image description here

The other issue is that the errorbars have position = "identity", which means they all sit at the same height instead of stacking. You get strange results when you try to stack errorbars to stack however. Although there are ways round this, the height issue means that these are not really worth attempting, because the errors are effectively too small to be shown on the plot.

If the size of the errors is an important thing for you to display, your best option might be to use coord_cartesian to zoom in to the relevant part of the y axis. The bars still needs to be dodged for this to work, though:

ggplot(aa, aes(group, mean, fill = group2)) 
  geom_col(width = 0.7, size = 0.2, alpha = 1, color = 'black', 
           position = position_dodge(width = 0.8))  
  geom_errorbar(aes(ymin =mean-se, ymax =mean   se, group = group2),
                width = 0.2, position = position_dodge(width = 0.8))  
  theme(legend.text=element_text(family="Times",size=10)) 
  labs(x ='' , y = 'Organic matter (mg/kg)', fill = 'sample')  
  theme_bw() 
  scale_y_continuous(expand = c(0, 0), limit = c(0,9)) 
  coord_cartesian(ylim = c(8, 9))  
  guides(fill=guide_legend(title = '')) 
  theme(panel.grid = element_blank(),  panel.background = element_blank()) 
  theme(
    text = element_text(family="Times",size=14), 
    plot.title = element_text(family="Times",size = 14),
    axis.text.x = element_text(family="Times",size = 14, colour= 'black'),
    axis.text.y = element_text(family="Times",size = 14, colour= 'black'),
    legend.text = element_text(family="Times",size = 14, colour= 'black')) 
  theme(axis.ticks.length = unit(0.1,'cm'),
        plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"))   
  scale_fill_nejm()

enter image description here

  • Related