Tuttavia possiamo voler rappresentare graficamente la media, la deviazione standard (media + DS, media - DS) e i valori massimo e minimo.
Di seguito il codice per rappresentare graficamente, usando ggplot2 tale versione del boxplot; si aggiunge al grafico anche la rappresentazione dei singoli valori come punti, spostati grazie alla funzione jitter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |