# Graphiken (Pakete graphics und ggplot2) library(ggplot2) #----------------------------------------------------------------- #----------------------------------------------------------------- library(HSAUR2) data("Forbes2000", package = "HSAUR2") # ---------------------------------- # 2.7 Simple Graphics # Formatierungsbefehle für Graphiken ?par # ----------------- # Histogramm # graphics par(mar = c(4, 4, 0, 0)) hist(Forbes2000$marketvalue, main = "", col = "darkgray", xlab="market value", ylab="absolute frequency") # ggplot2 qplot(Forbes2000$marketvalue, xlab="market value", ylab="absolute frequency") qplot(Forbes2000$marketvalue, xlab="market value", ylab="absolute frequency", binwidth=10) qplot(Forbes2000$marketvalue, xlab="market value", ylab="absolute frequency", binwidth=10, geom="histogram") ggplot(Forbes2000, aes(x=marketvalue)) + geom_histogram(binwidth=10) + xlab("market value") + ylab("absolute frequency") # ----------------- # Streudiagramm # graphics par(mar = c(4, 4, 0, 0)) plot(log(marketvalue) ~ log(sales), data = Forbes2000, pch = 16, col = rgb(0, 0, 0, 0.1)) ?rgb # rgb(red, green, blue, alpha, names = NULL, maxColorValue = 1) # Parameter alpha gibt die Transparenz an # ggplot2 qplot(log(sales), log(marketvalue), data=Forbes2000) qplot(log(sales), log(marketvalue), data=Forbes2000, geom="point") ggplot(Forbes2000, aes(x=log(sales), y=log(marketvalue))) + geom_point() ggplot(Forbes2000, aes(x=log(sales), y=log(marketvalue))) + geom_point(alpha=0.1,size=4,colour="blue") # ---------------------------------- # 2.8 Simple Linear Regression m1 <- lm(log(marketvalue) ~ log(sales), data = Forbes2000) # Einzeichnen der Regressionsgerade in das Streudiagramm # graphics par(mar = c(4, 4, 0, 0)) plot(log(marketvalue) ~ log(sales), data = Forbes2000, pch = 16, col = rgb(0, 0, 0, 0.1)) abline(m1, col = "red", lwd = 2) # ggplot2 ggplot() + geom_point(data=Forbes2000, aes(x=log(sales), y=log(marketvalue))) + geom_abline(slope=m1$coefficients[2], intercept=m1$coefficients[1], colour="red", size=2) #----------------------------------------------------------------- #----------------------------------------------------------------- # Weitere Funktionen zur Erstellung von Graphiken #----------------------- # Beispieldatensätze # Iris-Datensatz: # This famous (Fisher's or Anderson's) iris (Schwertlilien) data set gives the measurements in centimeters of the variables # sepal (Kelchblatt) length and width and petal (Blütenblatt) length and width, respectively, for 50 flowers # from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica. data("iris") ?iris # Titanic-Datensatz: # This data set provides information on the fate of passengers on the fatal maiden voyage of the ocean liner ‘Titanic’, # summarized according to economic status (class), sex, age and survival. data("Titanic") ?Titanic # table Titanic in data.frame mit Häufigkeiten umwandeln frequency <- data.frame("Frequency"=as.vector(Titanic)) covariates <- expand.grid(dimnames(Titanic)) table_Titanic <- cbind(covariates,frequency) # data.frame mit einzelnen Beobachtungen frequency <- as.vector(Titanic) covariates <- expand.grid(dimnames(Titanic)) Class <- rep(covariates[,1],frequency) Sex <- rep(covariates[,2],frequency) Age <- rep(covariates[,3],frequency) Survived <- rep(covariates[,4],frequency) data_Titanic <- data.frame("Class"=Class,"Sex"=Sex,"Age"=Age,"Survived"=Survived) #----------------------------------- # Säulen-/Balkendiagramm ?barplot addmargins( table(data_Titanic$Class, data_Titanic$Sex) ) # oder addmargins( apply(Titanic,c(1,2),sum) ) # Balken nebeneinander # graphics barplot(table(data_Titanic$Class, data_Titanic$Sex), beside=TRUE,legend=TRUE) # oder barplot(apply(Titanic,c(1,2),sum), beside=TRUE, legend=TRUE) # ggplot2 # Plot basierend auf Daten mit individuellen Beobachtungen ggplot(data_Titanic, aes(x=Sex, fill=Class)) + geom_bar(position="dodge", stat="bin") # Graphik-Optionen in ggplot # Graphik getrennt nach Gruppen ggplot(table_Titanic, aes(x=Class, y=Frequency)) + geom_bar(stat="identity") + facet_wrap(~Sex, nrow=1) # andere Farbe ggplot(table_Titanic, aes(x=Class, y=Frequency)) + geom_bar(stat="identity", fill="blue") + facet_wrap(~Sex, nrow=1) # Farben getrennt nach Gruppe ggplot(table_Titanic, aes(x=Class, y=Frequency)) + geom_bar(stat="identity", aes(fill=Sex)) + facet_wrap(~Sex, nrow=1) + scale_fill_manual(values=c("blue","red")) # Graphiken untereinander ggplot(table_Titanic, aes(x=Class, y=Frequency)) + geom_bar(stat="identity") + facet_wrap(~Sex, ncol=1) # weiteres Beispiel mit einer Gruppierungsvariable ggplot(table_Titanic, aes(x=Sex, y=Frequency)) + geom_bar(stat="identity") + facet_wrap(~Class) # Beispiele mit zwei Gruppierungsvariablen # funktioniert nur auf den Daten mit individuellen Beobachtungen xtabs(~Class+Sex+Age, data=data_Titanic) # Aufteilung nach Class und Age ggplot(data_Titanic, aes(x=Sex, fill=Class)) + geom_bar(position="dodge", stat="bin") + facet_wrap(~Age) # Balken übereinander ggplot(data_Titanic, aes(x=Sex, fill=Class)) + geom_bar(position="stack", stat="bin") + facet_wrap(~Age) # Farben variieren ggplot(data_Titanic, aes(x=Sex, fill=Class)) + geom_bar(position="dodge", stat="bin") + facet_wrap(~Age) + scale_fill_manual(values=c(grey(0.8), grey(0.6), grey(0.4), grey(0.2))) # Grid ggplot(data_Titanic, aes(x=Class)) + geom_bar(position="dodge", stat="bin") + facet_grid(Sex~Age) #----------------------------------- # Mosaikplot # graphics ?mosaicplot # basierend auf den Kreuztabellen mosaicplot(~ Sex + Survived, data = Titanic, shade = TRUE) # basierend auf den individuellen Beobachtungen mosaicplot(~ Sex + Survived, data = data_Titanic, shade = TRUE) #----------------------------------- # Boxplot # graphics ?boxplot boxplot(iris$Sepal.Length, iris$Sepal.Width, col=2:3, names=c("Sepal.Length", "Sepal.Width")) # ggplot2 help1 <- c(iris$Sepal.Length, iris$Sepal.Width) help2 <- data.frame("count"=help1, "which"=rep(c("Length","Width"), each=nrow(iris))) ggplot(help2, aes(x=which, y=count)) + geom_boxplot() # weitere Beispiele # mit Einfärbung ggplot(iris, aes(x=Species, y=Sepal.Length)) + geom_boxplot(fill="green", outlier.colour="blue", outlier.size=5) # Farben getrennt nach Gruppen ggplot(iris, aes(x=Species, y=Sepal.Length)) + geom_boxplot(aes(fill=Species)) # Spielereien ggplot(iris, aes(x=Species, y=Sepal.Length)) + geom_boxplot() + geom_jitter() + coord_flip() ggplot(iris, aes(x=Species, y=Sepal.Length)) + geom_boxplot(aes(colour=Species)) + geom_jitter(position = position_jitter(width = 0.05, height=0), aes(colour=Species)) + coord_flip() #----------------------------------- # Streudiagrammmatrix ?pairs pairs(iris, col=iris$Species) # Streudiagramm nach Gruppen library("lattice") xyplot(Petal.Length ~ Sepal.Length | Species, data=iris) # ggplot2 # Farben nach Gruppen ggplot(iris, aes(x=Petal.Length, y=Sepal.Length, colour=Species)) + geom_point() # Form nach Gruppen ggplot(iris, aes(x=Petal.Length, y=Sepal.Length, shape=Species)) + geom_point() # Farben und Form nach Gruppen ggplot(iris, aes(x=Petal.Length, y=Sepal.Length, colour=Species, shape=Species)) + geom_point(size=4) + scale_colour_manual(values=c(grey(0.8), grey(0.6), grey(0.4))) # Unterteilung nach Gruppen ggplot(iris, aes(x=Petal.Length, y=Sepal.Length)) + geom_point() + facet_wrap(~Species, nrow=2) ggplot(iris, aes(x=Petal.Length, y=Sepal.Length)) + geom_point() + facet_grid(Species~.) ggplot(iris, aes(x=Petal.Length, y=Sepal.Length)) + geom_point() + facet_grid(.~Species) # Farben nach weiterer metrischer Variable ggplot(iris, aes(x=Petal.Length, y=Sepal.Length, colour=Petal.Width)) + geom_point() ggplot(iris, aes(x=Petal.Length, y=Sepal.Length, colour=Petal.Width)) + geom_point() + scale_colour_gradientn(colours=rainbow(4))+ theme(legend.position="top") # Es ist auch möglich, nach mehreren Gruppierungsvariablen zu unterscheiden gruppierung <- rep(1:3, times=50) # willkürliche Beispielgruppierung gruppierung <- factor(gruppierung, levels=1:3, labels=c("Gruppe 1", "Gruppe 2", "Gruppe 3")) xyplot(Petal.Length ~ Sepal.Length | Species * gruppierung, data=iris) ggplot(cbind(iris, gruppierung), aes(x=Petal.Length, y=Sepal.Length, colour=Species)) + geom_point() + facet_wrap(~gruppierung) ggplot(cbind(iris, gruppierung), aes(x=Petal.Length, y=Sepal.Length)) + geom_point() + facet_grid(gruppierung~Species) #----------------------------------- # Parallele Koordinaten library("MASS") ?parcoord parcoord(iris[,1:4], col=iris$Species) #----------------------------------------------------------------- #----------------------------------------------------------------- # Einen Plot von Grund auf erstellen (mit graphics) # Beispieldaten: zwei Variablen a und b a <- seq(-5,5) b <- seq(-5,5) ?plot.new # advance to a new graphics frame ?plot.window # sets up the world coordinate system for a graphics window plot.new() plot.window(xlim=range(a),ylim=range(b)) points(a,b,pch=19) lines(a,b) axis(1) axis(2) box() #----------------------- # Weitere nützliche Befehle zur Erstellung von eigenen Graphiken ?par ?text # Text in der Graphik ?mtext # Text in den Rändern ?box op <- par(mar=c(4,4,4,4), oma=c(2,2,2,2)) # (inner) margins und outer margins # Plot plot(a, b, xlab="a", ylab="b", pch=19) box("figure", lty="dashed", col="orange") # Plot area box("plot", lty="solid", col="red") text(x=4, y=-4, labels="Plot Area", col="red", cex=1.5) # Inner margin area box("inner",lty="dashed", col="blue") mtext("Inner Margin Area", side=3, line=1, adj=1, cex=1.5, col="blue", outer=FALSE) # Outer margin area box("outer", lty="solid", col="green",lwd=4) mtext("Outer Margin Area", side=1, line=0.5, adj=1, cex=1.5, col="green", outer=TRUE) # Zusätzliche Beschriftung mtext("Rechts", 4, at=2, line=0.25, cex=1.2, col=grey(0.4), las=1) # Grafikeinstellungen auf Standard zurücksetzen par(op) #----------------------- # mehrere Graphiken in einem Fenster par(mfrow=c(2,2)) mosaicplot(~ Class + Survived, data = Titanic) mosaicplot(~ Sex + Survived, data = Titanic) mosaicplot(~ Age + Survived, data = Titanic) mosaicplot(~ Class + Sex + Age, data = Titanic) # library(gridExtra) für mehrere ggplot2 Graphiken in einem Fenster library(gridExtra) plot1 <- ggplot(iris, aes(x=Petal.Length, y=Sepal.Length)) + geom_point() + facet_grid(Species~.) plot2 <- ggplot(iris, aes(x=Petal.Length, y=Sepal.Length)) + geom_point() + facet_grid(.~Species) grid.arrange(plot1,plot2,nrow=1) ?layout layout(mat=matrix(c(1,2,3,0), nrow=2, ncol=2, byrow=TRUE) ) plot(Petal.Length ~ Sepal.Length, data=iris ) boxplot(iris$Petal.Length, ylab="Petal.Length") boxplot(iris$Sepal.Length, xlab="Sepal.Length", horizontal=TRUE) layout(mat=matrix(c(1,2,3,0), nrow=2, ncol=2, byrow=TRUE), widths=c(2.5,1.1), heights=c(2.5,1.1) ) plot(Petal.Length ~ Sepal.Length, data=iris ) boxplot(iris$Petal.Length, ylab="Petal.Length") boxplot(iris$Sepal.Length, xlab="Sepal.Length", horizontal=TRUE) layout(mat=matrix(c(3,0,1,2), nrow=2, ncol=2, byrow=TRUE), widths=c(2,1), heights=c(1,2) ) plot(Petal.Length ~ Sepal.Length, data=iris ) boxplot(iris$Petal.Length, ylab="Petal.Length") boxplot(iris$Sepal.Length, xlab="Sepal.Length", horizontal=TRUE) # layout-Einstellungen löschen dev.off() #----------------------- # Speichern von Graphiken setwd("S:/ProgStat") # als pdf ?pdf pdf(file="Ergebnisse/boxplot.pdf", width=7, height=7) boxplot(iris$Sepal.Length, iris$Sepal.Width, col=2:3, names=c("Sepal.Length", "Sepal.Width")) dev.off() # als postscript postscript(file="Ergebnisse/boxplot.ps", width=7, height=7) boxplot(iris$Sepal.Length, iris$Sepal.Width, col=2:3, names=c("Sepal.Length", "Sepal.Width")) dev.off() # als png png(filename = "Ergebnisse/boxplot.png", width = 480, height = 480) boxplot(iris$Sepal.Length, iris$Sepal.Width, col=2:3, names=c("Sepal.Length", "Sepal.Width")) dev.off() # als Windows-Metafile (funktioniert nur unter Windows) win.metafile(filename = "Ergebnisse/boxplot.emf", width=7, height=7) boxplot(iris$Sepal.Length, iris$Sepal.Width, col=2:3, names=c("Sepal.Length", "Sepal.Width")) dev.off()