RSocialScience

Kurs zur Nutzung von R in den Sozialwissenschaften

Ein Plot sagt mehr als 1000 Worte

Plot ist nicht gleich Plot

Task View zu Thema Graphiken

Datensatz

library(mlmRev)
data(Chem97)

Histogramm - Die Funktion hist()

Wir erstellen ein Histogramm der Variable gcsescore:

?hist

hist(Chem97$gcsescore)

Graphik speichern

Befehl um Graphik zu speichern

png("Histogramm.png")
hist(Chem97$gcsescore)
dev.off()

Histogramme

Argument Bedeutung Beispiel
main Überschrift main="Hallo Welt"
xlab x-Achsenbeschriftung xlab="x-Werte"
ylab y-Achsenbeschriftung ylab="y-Werte"
col Farbe col="blue"

Histogramm

hist(Chem97$gcsescore,col="blue",
     main="Hallo Welt",ylab="y-Werte", xlab="x-Werte")

Weitere Argumente:

?plot
# oder
?par

Barplot

tabScore <- table(Chem97$score)

barplot(tabScore)

Barplots und barcharts

barplot(tabScore)

Mehr Farben:

barplot(tabScore,col=rgb(0,0,1))

Grüne Farbe

barplot(tabScore,col=rgb(0,1,0))

Rote Farbe

barplot(tabScore,col=rgb(1,0,0))

Transparent

barplot(tabScore,col=rgb(1,0,0,.3))

Scatterplots

Beispieldaten für Scatterplot

x <- runif(100)
y <- runif(100)

Einfacher Scatterplot

plot(x,y)

Einfacher Scatterplot II

plot(x,y,pch=20)

Einfacher Scatterplot III

plot(x,y,pch=20)

Boxplot

?boxplot

Horizontaler Boxplot

boxplot(Chem97$gcsescore,
horizontal=TRUE)

Gruppierte Boxplots

Beispiel grupierter Boxplot

boxplot(Chem97$gcsescore~Chem97$gender)

Alternativen zu Boxplot

Violinplot

# Beispieldaten erzeugen
x <- rnorm(100)
y <- rnorm(100)

Die Bibliothek vioplot

library(vioplot)
plot(x, y, xlim=c(-5,5), ylim=c(-5,5))
vioplot(x, col="tomato", horizontal=TRUE, at=-4, 
        add=TRUE,lty=2, rectCol="gray")
vioplot(y, col="cyan", horizontal=FALSE, at=-4, 
        add=TRUE,lty=2)

vioplot - Das Ergebnis

Alternativen zum Boxplot

library(beanplot)
par(mfrow = c(1,2))
boxplot(count~spray,data=InsectSprays,col="blue")
beanplot(count~spray,data=InsectSprays,col="orange")

CMYK Farbschema

pdf("test.cmyk.pdf", colormodel='cmyk')
pie(1:10, col=1:10)
dev.off() 

Aufgabe - einfache Grafiken