Adding error bar and mean value in box plot with multiple variables

I have tried to create box plot of multiple variables ( including error bar and mean value) with the following data set and code:

GD As(III) As(V) DMA
SD 204.67 111.83 28.50
SD 147.99 83.52 32.00
SD 180.14 89.91 22.37
SD 174.77 82.58 28.65
SD 237.04 116.60 30.76
SD 223.24 117.23 32.25
SD 207.66 185.22 23.01
SD 227.52 168.53 27.29
SD 245.76 95.22 39.41
SD 223.32 98.36 37.17
SD 266.18 139.42 39.67
SD 254.49 166.25 44.87
SD 243.29 130.25 34.16
SD 232.71 175.33 31.37
SD 204.41 102.33 23.84
SD 159.67 107.82 17.73
SD 222.93 302.68 24.31
SD 0.00 141.30 25.10
SD 182.18 359.29 19.13
SD 185.37 314.82 23.73
SD 117.30 135.41 18.80
SD 115.27 219.87 17.42
SD 150.64 104.76 19.38
SD 136.07 84.40 19.96
SD 97.47 95.46 33.38
SD 107.55 87.37 29.10
SD 192.28 111.07 25.23
SD 190.13 125.81 22.26
SD 113.75 89.98 21.41
SD 99.76 75.95 14.55
SD 126.90 83.13 47.34
SD 108.55 95.68 49.64
SD 162.66 111.95 29.89
SD 170.36 129.30 24.89
SD 170.24 161.31 11.14
SD 159.10 141.48 12.43
SD 190.52 90.04 23.63
SD 176.07 85.61 23.53
SD 156.56 71.10 20.82
SD 144.60 98.02 15.82
SD 105.61 219.65 18.45
SD 122.87 209.81 19.38
LD 204.04 146.75 20.52
LD 192.27 122.13 21.41
LD 180.67 90.54 29.72
LD 181.65 61.29 28.14
LD 152.31 117.45 26.06
LD 156.84 140.16 21.86
LD 196.73 121.18 43.37
LD 201.56 112.01 59.25
LD 150.18 47.71 111.86
LD 149.25 59.57 98.83
LD 301.24 107.84 30.24
LD 222.81 84.93 22.54
LD 167.38 160.23 26.64
LD 152.51 179.93 34.64
LD 116.70 74.41 37.27
LD 100.91 97.41 36.36
LD 150.13 124.40 24.46
LD 169.49 158.51 31.84
LD 128.48 148.99 41.13
LD 124.93 131.18 41.37
LD 110.73 63.25 33.26
LD 113.17 306.77 28.49
LD 93.32 105.54 34.69
LD 105.35 139.07 30.22
LD 136.99 65.36 31.13
LD 130.23 78.49 32.98
LD 93.45 59.23 48.78
LD 102.94 67.61 43.74
LD 179.26 83.71 32.27
LD 152.09 105.83 29.34
LD 128.67 96.53 45.21
LD 151.38 119.54 47.58
LD 121.15 180.54 48.02
LD 109.91 131.97 39.61
LD 105.32 112.96 39.58
LD 107.44 93.17 36.49
LD 124.15 146.74 44.26
LD 134.95 248.19 46.99
LD 103.87 83.19 52.79
LD 103.97 106.20 52.71
LD 218.52 63.01 35.61
LD 247.60 40.58 30.25
LD 166.14 52.53 39.89
LD 160.68 62.15 46.89
LD 164.12 31.30 20.38
LD 163.71 32.98 20.36
LD 132.94 169.62 16.55
LD 111.15 174.36 19.24
LD 86.32 164.64 16.61
LD 81.49 167.59 18.59

library(readxl)
Data<-read_excel("Speciation.xlsx", sheet = "Sheet1")
Data
library(reshape2)
Data_long <- melt(Data, id = "GD" )
head(Data_long)

library(ggplot2)
As <- ggplot(Data_long, aes(x = variable,y = value, fill = GD))+
geom_boxplot()+
stat_boxplot(geom = "errorbar", width = 0.2, color="black") +
stat_summary(fun = "mean", geom = "point", size = 1.5, col = "red")

As
But the resulting plot looks below where the error bar and mean value are out of box:

Please help to fix this.

Try with explicitly indicating the position:

ggplot(Data_long, aes(x = variable,y = value, fill = GD))+
  geom_boxplot()+
  stat_boxplot(geom = "errorbar", width = 0.2, color="black", position = position_dodge(.75)) +
  stat_summary(fun = "mean", geom = "point", size = 1.5, col = "red", position = position_dodge(.75))

Just a note: while it can make sense to indicate the mean on top of a boxplot (which by default uses the median), I'm not sure what is your goal with the error bars: here you use the same bars that are already drawn by geom_boxplot(), they don't really bring any useful information.

Dear ALexsW,
Thank you very much. It's worked perfectly.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.