programmieren in anwendungen - softech.informatik.uni … · uberblick einleitung die funktionen...
TRANSCRIPT
Programmieren in Anwendungen
Jan Starz
Technische Universitat Kaiserslautern
20.06.2013
1 / 33
Uberblick
Einleitung
Die Funktionen qplot und ggplot
Data frame “diamonds“
Der Umgang mit qplot
Der Umgang mit ggplot
ggplot - Nutzliche Geometrien
2 / 33
Einleitung
3 / 33
Was ist ggplot2?
I Ein R-Package fur Datenvisualisierung
I Unkompliziert, intuitive Benutzung, ermoglicht Modifikationder 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 via library(ggplot2)
4 / 33
Die Funktionen qplot und ggplot
5 / 33
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 Fur alle folgenden Layer gultig
I Inkrementelle PloterstellungI Addition von Anweisungen mithilfe von +I Z.B. geom point(), geom bar(), xlab(“X-Achse“),
facet grid(...)
7 / 33
Data frame “diamonds“
8 / 33
Struktur von “diamonds“
I Standardmaß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 ...
9 / 33
Der Umgang mit qplot
10 / 33
Der erste Plot
qplot(data=diamonds, price)
11 / 33
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")
13 / 33
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)
15 / 33
Der Umgang mit ggplot
16 / 33
“No layers in plot“
myPlot <- ggplot(data=diamonds, aes(x=price))
17 / 33
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")
19 / 33
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))
21 / 33
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 dieeinzelnen Kategorien eines Parameters:
+ facet grid(horizontal ∼ vertical)
I Gruppiere in der Horizontalen nach den Werten von cutfacet grid(cut ∼ .)
I Gruppiere in der Vertikalen nach den Werten von cutfacet grid(. ∼ cut)
+ facet wrap(∼ vertical)
I Automatische Platzierung der Facetten neben- unduntereinander
I Parameter ncol=x begrenzt das Wrapping horizontal
23 / 33
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)
25 / 33
ggplot - Nutzliche Geometrien
26 / 33
Saulendiagramm
myPlot2 <- ggplot(data=diamonds, aes(x=clarity))
+ geom bar(
data=w, (Definition eigener Data frame)
width=x, (Einstellung der Breite)
aes(fill=y), (Fullfarbe der Balken abhangig von y)
position=“z“ (Anordnung der gefarbten Balken)I Moglich sind “stack“ (default), “dodge“, “fill“ und “identity“
)
27 / 33
Saulendiagramm, Beispiel
(a) aes(fill=cut), position=“stack“ (b) aes(fill=cut), position=“dodge“
28 / 33
Kreisdiagramme, spezielle Saulendiagramme+ 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
coun
t
0.8
0.8
color
D
E
F
G
H
I
J
(c)
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
(d) theta=“y“
29 / 33
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")
0
5000
10000
I1 SI2 SI1 VS2 VS1 VVS2 VVS1 IFclarity
coun
t
cut
Fair
Good
Very Good
Premium
Ideal
31 / 33
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 IFclarity
dens
ity
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 Idealcut
pric
e
cut
Fair
Good
Very Good
Premium
Ideal
0.5
0.5
(e) geom violin() (f) geom polygon()
33 / 33