How to plot mean and standard error in Boxplot in R

I have two categorical factors (‘Habitat’ and ‘Locality’), and one continuous variable (T). ‘Habitat’ has two level and ‘Locality’ has eight levels. I want to change the default whiskers to represent the SE, and the median into the mean for each boxplot. Is there a way to do this and taking both of the categorical factors into account when plotting? Many thanks in advance.

This is what I have done with the default setting of boxplot ggplot, showing the first and third quartiles with median intervals.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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')
</code>
<code>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') </code>
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')

First write a function that compute the min, mean-1SEM, mean, mean+1SEM, and Max. Then map these 5 values onto a boxplot using stat_summary.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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)
</code>
<code>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) </code>
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)

1

I expect that it is possible, but it is also possible to put up a traffic sign that is a red octagon and says “Increased speed limit ahead”, I expect that both will be more confusing that helpful. The boxplot has a standard definition of what the parts represent. When a user sees a boxplot they should not have to go through extra mental gymnastics to rethink what the different parts mean. Why not use a different representation if you don’t want to represent these standard summaries. The geom_crossbar or geom_errorbar function/geoms may be more appropriate for your display (and probably easier to use than trying to modify the boxplot geom).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật