Programmieren in Anwendungen
Jan St¨arz
Technische Universit¨at Kaiserslautern j staerz11@cs.uni-kl.de
20.06.2013
Uberblick ¨
Einleitung
Die Funktionen qplot und ggplot Data frame “diamonds“
Der Umgang mit qplot Der Umgang mit ggplot ggplot - N¨utzliche Geometrien
2 / 33
Einleitung
Was ist ggplot2?
I Ein R-Package f¨ur Datenvisualisierung
I Unkompliziert, intuitive Benutzung, erm¨oglicht Modifikation der Plotkomponenten selbst auf hoher Abstraktionsebene
I Hauptfunktionen sind qplot und ggplot
I Arbeitet mit Layern (Schichten)
I Installation via install.packages(“ggplot2“)
I Einbindung in den Workspace vialibrary(ggplot2)
4 / 33
Die Funktionen qplot und ggplot
qplot - Quick plot
qplot(data,x,y= NULL, ..., facets = NULL,
margins = FALSE,geom = “auto“, stat = list(NULL), position = list(NULL), xlim = c(NA, NA),
ylim = c(NA, NA), log = ““, main = NULL, xlab = deparse(substitute(x)),
ylab = deparse(substitute(y)), asp = NA)
I Meistgenutzte Parameter in rot!
6 / 33
ggplot - komplexere Plots
ggplot(data = NULL, aes)
I aes(x, y, color, alpha, size,...)
I Wie Datenvariablen auf visuelle Eigenschaften der benutzten Geometrie ¨ubertragen werden (¨Asthetik)
I F¨ur alle folgenden Layer g¨ultig
I Inkrementelle Ploterstellung
I Addition von Anweisungen mithilfe von +
I Z.B. geom point(), geom bar(), xlab(“X-Achse“), facet grid(...)
Data frame “diamonds“
8 / 33
Struktur von “diamonds“
I Standardm¨aßig im ggplot2-Package enthalten
I Beinhaltet Eigenschaften zu fast 54.000 Diamanten
> head(diamonds)
carat cut color clarity depth table price ...
1 0.23 Ideal E SI2 61.5 55 326 ...
2 0.21 Premium E SI1 59.8 61 326 ...
3 0.23 Good E VS1 56.9 65 327 ...
4 0.29 Premium I VS2 62.4 58 334 ...
5 0.31 Good J SI2 63.3 58 335 ...
6 0.24 Very Good J VVS2 62.8 57 336 ...
Der Umgang mit qplot
10 / 33
Der erste Plot
qplot(data=diamonds, price)
Der erste Plot - Version 2
qplot(data=diamonds, price, carat)
12 / 33
Der erste Plot - Version 3
qplot(data=diamonds, price, carat, main="ggPlot2!", xlab="Preis", ylab="Karat")
Der erste Plot - Version 4
qplot(data=diamonds, price, carat, color=color)
14 / 33
Der erste Plot - Version 5
qplot(data=diamonds, price, carat, color=color, alpha=cut)
Der Umgang mit ggplot
16 / 33
“No layers in plot“
myPlot <- ggplot(data=diamonds, aes(x=price))
Der analoge ggPlot
myPlot <- myPlot + geom_bar()
18 / 33
Der analoge ggPlot - Version 2
myPlot <- ggplot(data=diamonds, aes(x=price, y=carat)) myPlot + geom_point()
+ ggtitle("ggPlot2") + xlab("Preis") + ylab("Karat")
Der analoge ggPlot - Version 3
myPlot + geom_point(aes(color=color))
20 / 33
Der analoge ggPlot - Version 4
myPlot + geom_point(aes(color=color, alpha=cut))
Der analoge ggPlot - Version 5
myPlot + geom_point(aes(color=color, alpha=cut)) + coord_flip()
22 / 33
Einschub - Facetten
Facetten dienen der Aufsplitterung von Diagrammen in die einzelnen Kategorien eines Parameters:
+ facet grid(horizontal ∼ vertical)
I Gruppiere in der Horizontalen nach den Werten von cut facet grid(cut ∼.)
I Gruppiere in der Vertikalen nach den Werten von cut facet grid(. ∼cut)
+ facet wrap(∼ vertical)
I Automatische Platzierung der Facetten neben- und untereinander
I Parameter ncol=xbegrenzt das Wrapping horizontal
Der analoge ggPlot - Version 6
myPlot + geom_point(aes(color=color, alpha=cut)) + coord_flip() + facet_wrap(~ cut)
24 / 33
Der analoge ggPlot - Version 7
myPlot + geom_smooth(aes(color=color, alpha=cut)) + coord_flip() + facet_wrap(~ cut)
ggplot - N¨ utzliche Geometrien
26 / 33
S¨ aulendiagramm
myPlot2 <- ggplot(data=diamonds, aes(x=clarity))
+ geom bar(
data=w, (Definition eigener Data frame) width=x, (Einstellung der Breite)
aes(fill=y), (F¨ullfarbe der Balken abh¨angig von y) position=“z“ (Anordnung der gef¨arbten Balken)
I M¨oglich sind “stack“ (default), “dodge“, “fill“ und “identity“
)
S¨ aulendiagramm, Beispiel
(a) aes(fill=cut), position=“stack“ (b) aes(fill=cut), position=“dodge“
28 / 33
Kreisdiagramme, spezielle S¨ aulendiagramme
+ geom bar() + coord polar(
theta=“x“ (welche Achse den Kreisumfang bildet, “x“/“y“) )
Fair
Good
Very Good Premium
Ideal
0 5000 10000 15000 20000
cut
count
0.8 0.8
color D E F G H I J
0
5000
10000 15000
20000
Fair Good Very Good Premium Ideal
count
cut
0.8 0.8
color D E F G H I J
Frequency polygon
geom freqpoly(
data=w, (Definition eigener Data frame) aes(group=x, (Linien nach Kriterium x) color=x), (Farben der Kriterien)
position=“z“ (Anordnung der Linien) )
30 / 33
Frequency polygon, Beispiel
myPlot2 + geom_freqpoly(aes(group=cut, color=cut), position="stack")
5000 10000
count
cut Fair Good Very Good Premium Ideal
Dichteauswertung “density“
myPlot2 + geom_density(aes(fill=clarity, alpha=0.5))
0.0 0.5 1.0 1.5
I1 SI2 SI1 VS2 VS1 VVS2 VVS1 IF
clarity
density
clarity I1 SI2 SI1 VS2 VS1 VVS2 VVS1 IF
0.5 0.5
32 / 33
...und viele mehr
0 5000 10000 15000
Fair Good Very Good Premium Ideal
cut
price
cut Fair Good Very Good Premium Ideal
0.5 0.5
(e) geom violin() (f) geom polygon()