R-ohjelmointi.org
Tilastotieteellistä ohjelmointia R-kielellä
Boxplot-kuvien tekeminen R:llä
Usein aineiston jakauman tarkasteluun suositellaan boxplot-kuvaajaa. Ohessa kolme esimerkkiä, miten kyseisen kuvaajan voi R:llä piirtää. Ensimmäinen on toteutettu ns. base-grafiikalla, eli R:n perinteisellä grafiikkamoottorilla, toinen lattice-paketilla ja kolmas ggplot2-grafiikalla.
#Selitteet mainhead <- "Boxplot-esimerkki" color <- "magenta" ylabel <- "count" xlabel <- "spray" #--------------------- base graphic- boxplot ------------------- #png("boxplot_base.png", width=600, height=600) boxplot(count ~ spray, data = InsectSprays, boxwex = 0.25) grid(col="black") par(new=TRUE) g <- boxplot(count ~ spray, data = InsectSprays, boxwex = 0.25, col = color, main = mainhead, ylab=ylabel, xlab=xlabel) selitteet <- c("min", ".25", "median", ".75", "max") text(1:6-0.5, t(g$stats), labels=rep(selitteet, each=6), cex=0.7) arrows(1:6-0.3, t(g$stats), 1:6-0.1, t(g$stats), length=0.1, col="red") #dev.off() |
#--------------------- lattice- boxplot ------------------- library(lattice) #png("boxplot_lattice.png", width=600, height=600) bwplot(count ~ spray, data=InsectSprays, xlab = xlabel, ylab = ylabel, main = mainhead, fill = color ) #dev.off() |
#--------------------- ggplot2- boxplot ------------------- library(ggplot2) #png("boxplot_ggplot2.png", width=600, height=600) p <- ggplot(InsectSprays, aes(factor(spray), count)) + xlab(xlabel) + ylab(ylabel) p + geom_boxplot(fill = color) + opts(title = mainhead) #dev.off() |
Vastaa