How to build medium and standard error in Boxplot in R

I have two categorical factors ("Habitat" and "Locality") and one continuous variable (T). Habitat has two levels, and Terrain has eight levels. I want to change the default ears for representing SE, and the median on average for each boxplot. Is there a way to do this and consider both categorical factors when plotting? Thanks in advance.

This is what I did with the default ggplot boxplot setting showing the first and third quartiles at mid intervals.

ggplot(data,aes(x=Locality,y=T)) + 
  geom_boxplot(aes(fill=interaction(Habitat,Locality), 
                   group=interaction(factor(Habitat),Locality)),
               outlier.shape=1,outlier.size=3) + 
  theme_bw() + 
  theme(
    panel.grid.major=element_blank(),
    panel.grid.minor=element_blank(),
    axis.line=element_line(colour='black'),
    legend.position='none',
    axis.text.x=element_text(angle=90,hjust=1,size=12)) + 
  scale_y_continuous('T') + 
  xlab('Locality')
+4
source share
2 answers

, min, mean-1SEM, mean, mean + 1SEM Max. 5 stat_summary.

library(gridExtra)
library(ggplot2)

MinMeanSEMMax <- function(x) {
  v <- c(min(x), mean(x) - sd(x)/sqrt(length(x)), mean(x), mean(x) + sd(x)/sqrt(length(x)), max(x))
  names(v) <- c("ymin", "lower", "middle", "upper", "ymax")
  v
}

g1 <- ggplot(mtcars, aes(factor(am), mpg)) + geom_boxplot() +
  ggtitle("Regular Boxplot")

g2 <- ggplot(mtcars, aes(factor(am), mpg)) +
  stat_summary(fun.data=MinMeanSEMMax, geom="boxplot", colour="red") + 
  ggtitle("Boxplot: Min, Mean-1SEM, Mean, Mean+1SEM, Max")


grid.arrange(g1, g2, ncol=2)

enter image description here

+10

, , , , " ", , , . , . , , , . , . / geom_crossbar geom_errorbar (, , , boxplot).

+3

All Articles