簡體   English   中英

ggplot:具有填充和標准錯誤的箱線圖

[英]ggplot: Boxplot with fill and standard errors

這是箱線圖

ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + geom_boxplot()

在此處輸入圖片說明

我想重現相同的圖,顯示平均值,一個標准誤差和兩個標准誤差,而不是中位數和分位數。


我做了

boxes <- function(x) {
  #r <- quantile(x, probs = c(0.05, 0.25, 0.5, 0.75, 0.95))
  meanx = mean(x)
  n = length(x)
  se = sqrt(var(x)/n)
  r <- c( meanx-1.96*se, meanx-se, meanx, meanx+se, meanx+1.96*se )
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + stat_summary(fun.data=boxes, geom="boxplot")

在此處輸入圖片說明

問題在於,不同顏色的框重疊而不是並排放置。

position ='dodge'應該可以解決您的問題。

ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + stat_summary(fun.data=boxes, geom="boxplot", position = 'dodge')

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM