Artificial Intelligence Programming Lab(AIPLab) 討論區

Please login or register.

請輸入帳號, 密碼以及預計登入時間

新聞:

[慶賀]恭喜亞大獲《泰晤士報》亞洲最佳大學排名第83名,國內排名第十名-20170201

作者 主題: R Boxplot with mean and standard deviation in ggPlot2  (閱讀 1028 次)

admin

  • 管理員
  • Hero Member
  • *****
  • 文章: 1838
    • 檢視個人資料
R Boxplot with mean and standard deviation in ggPlot2
« 於: 四月 02, 2017, 03:30:37 am »
https://www.r-bloggers.com/boxplot-with-mean-and-standard-deviation-in-ggplot2-plus-jitter/
程式碼: [Select]
library(ggplot2)

# create fictitious data
a <- runif(10)
b <- runif(12)
c <- runif(7)
d <- runif(15)

# data groups
group <- factor(rep(1:4, c(10, 12, 7, 15)))

# dataframe
mydata <- data.frame(c(a,b,c,d), group)
names(mydata) <- c("value", "group")

# function for computing mean, DS, max and min values
min.mean.sd.max <- function(x) {
  r <- c(min(x), mean(x) - sd(x), mean(x), mean(x) + sd(x), max(x))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}

# ggplot code
p1 <- ggplot(aes(y = value, x = factor(group)), data = mydata)
p1 <- p1 + stat_summary(fun.data = min.mean.sd.max, geom = "boxplot") + geom_jitter(position=position_jitter(width=.2), size=3) + ggtitle("Boxplot con media, 95%CI, valore min. e max.") + xlab("Gruppi") + ylab("Valori")
已記錄
 

SimplePortal Classic 2.0.5