Making grouped boxplots with ggplot2: R does not separate in groups

This is my data set:

Year Area s mean sd se
1 2004 Gootebank 9 0.2158556 0.1188472 0.03961573
2 2004 Thornton 4 1.9564700 1.9369257 0.96846283
3 2017 Gootebank 13 1.0664641 1.7131108 0.47513144
4 2017 Thornton 10 1.9384720 2.3308575 0.73708186
5 2018 Gootebank 13 1.4408562 1.6793808 0.46577643
6 2018 Thornton 10 1.8738613 3.0972972 0.97945136

I am trying to make a grouped boxplot that presents the mean values shown, in the table for each sandbank, for each year (which is 2004, 2017 and 2018 here). So should be a simple grouped boxplot. Im new with ggplot2 and can't find a way to make R separate each year in the plot

R seems to take the years together. I tried filling 'Area' as a factor but I can't seem to change it.
(incomplete) code up until now:

Sdata_autumnwinter <- ddply(epi_diverse_autumnwinter, c("Year", "Area"), summarise,
s = length(TOTALB),
mean = mean(TOTALB),
sd = sd(TOTALB),
se = sd / sqrt(s)
)
p <- ggplot(Sdata_autumnwinter, aes(x=Year, y=mean, fill= factor(Area))) +
geom_boxplot() + scale_x_discrete()+theme_bw()+ scale_y_continuous(limits=c(0, 3)) + labs(fill = "Area")

p

I'm not super confident about what you hope to see but perhaps this gets closer ?
Also could you gather the actual min and max values for your samples ? I fake them to the boxplot by assuming they are at 3 standard deviations

library(ggplot2)


tmp <- tempfile()

writeLines("Year Area s mean sd se
1 2004 Gootebank 9 0.2158556 0.1188472 0.03961573
2 2004 Thornton 4 1.9564700 1.9369257 0.96846283
3 2017 Gootebank 13 1.0664641 1.7131108 0.47513144
4 2017 Thornton 10 1.9384720 2.3308575 0.73708186
5 2018 Gootebank 13 1.4408562 1.6793808 0.46577643
6 2018 Thornton 10 1.8738613 3.0972972 0.97945136",
           tmp)

df<- read.table(tmp)

ggplot(df) +
  geom_boxplot(aes(x=paste0(Year,Area),
                   ymax=mean+sd*3,
                   upper = mean+sd*2,
                   y = mean,
                   middle = mean, 
                   lower = mean-sd*2,
                   ymin =mean-sd*3,),
                   stat = "identity")

Thank you very much for your reply, yes that definately gets closer!

I was hoping to obtain a boxplot similar to this:
image

Where x-axis is the years 2014, 2017 and 2018, y axis the mean and the subgroups are the sandbanks Goote & Thornton. I thought using "fill= factor(Area)" in aes() would be sufficient for R to recognize the subgroups for each year.

ggplot(df) +
  geom_boxplot(aes(x=Area,
                   fill=factor(Year),
                   ymax=mean+sd*3,
                   upper = mean+sd*2,
                   y = mean,
                   middle = mean, 
                   lower = mean-sd*2,
                   ymin =mean-sd*3,),
               stat = "identity")

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