Producing a boxplot in ggplot2 using summary statistics

[亡魂溺海] 提交于 2019-12-18 15:37:33

问题


Below is a code for producing a boxplot using ggplot2 I'm trying to modify in order to suit my problem:

library(ggplot2)
set.seed(1)
# create fictitious data
a <- rnorm(10)
b <- rnorm(12)
c <- rnorm(7)
d <- rnorm(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") + ggtitle("Boxplot con media, 95%CI, valore min. e max.") + xlab("Gruppi") + ylab("Valori")

In my case I do not have the actual data points but rather only their mean and standard deviation (the data are normally distributed). So for this example it will be:

mydata.mine = data.frame(mean = c(mean(a),mean(b),mean(c),mean(d)),sd = c(sd(a),sd(b),sd(c),sd(d)),group = c(1,2,3,4))

However I would still like to produce a boxplot. I thought of defining: ymin = mean - 3*sd lower = mean - sd mean = mean upper = mean + sd
ymax = mean + 3*sd

but I don't know how to define a function that will access mean and sd of mydata.mine from fun.data in stat_summary. Alternatively, I can just use rnorm to draw points from a normal parameterized by the mean and sd I have, but the first option seems to me a bit more elegant and simple.


回答1:


ggplot(mydata.mine, aes(x = as.factor(group))) +
  geom_boxplot(aes(
      lower = mean - sd, 
      upper = mean + sd, 
      middle = mean, 
      ymin = mean - 3*sd, 
      ymax = mean + 3*sd),
    stat = "identity")



来源:https://stackoverflow.com/questions/22212885/producing-a-boxplot-in-ggplot2-using-summary-statistics

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!