programmieren in anwendungen - softech.informatik.uni … · uberblick einleitung die funktionen...
TRANSCRIPT
![Page 1: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/1.jpg)
Programmieren in Anwendungen
Jan Starz
Technische Universitat Kaiserslautern
20.06.2013
1 / 33
![Page 2: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/2.jpg)
Uberblick
Einleitung
Die Funktionen qplot und ggplot
Data frame “diamonds“
Der Umgang mit qplot
Der Umgang mit ggplot
ggplot - Nutzliche Geometrien
2 / 33
![Page 3: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/3.jpg)
Einleitung
3 / 33
![Page 4: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/4.jpg)
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
![Page 5: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/5.jpg)
Die Funktionen qplot und ggplot
5 / 33
![Page 6: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/6.jpg)
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
![Page 7: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/7.jpg)
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
![Page 8: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/8.jpg)
Data frame “diamonds“
8 / 33
![Page 9: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/9.jpg)
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
![Page 10: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/10.jpg)
Der Umgang mit qplot
10 / 33
![Page 11: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/11.jpg)
Der erste Plot
qplot(data=diamonds, price)
11 / 33
![Page 12: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/12.jpg)
Der erste Plot - Version 2
qplot(data=diamonds, price, carat)
12 / 33
![Page 13: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/13.jpg)
Der erste Plot - Version 3
qplot(data=diamonds, price, carat, main="ggPlot2!",
xlab="Preis", ylab="Karat")
13 / 33
![Page 14: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/14.jpg)
Der erste Plot - Version 4
qplot(data=diamonds, price, carat, color=color)
14 / 33
![Page 15: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/15.jpg)
Der erste Plot - Version 5
qplot(data=diamonds, price, carat, color=color,
alpha=cut)
15 / 33
![Page 16: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/16.jpg)
Der Umgang mit ggplot
16 / 33
![Page 17: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/17.jpg)
“No layers in plot“
myPlot <- ggplot(data=diamonds, aes(x=price))
17 / 33
![Page 18: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/18.jpg)
Der analoge ggPlot
myPlot <- myPlot + geom_bar()
18 / 33
![Page 19: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/19.jpg)
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
![Page 20: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/20.jpg)
Der analoge ggPlot - Version 3
myPlot + geom_point(aes(color=color))
20 / 33
![Page 21: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/21.jpg)
Der analoge ggPlot - Version 4
myPlot + geom_point(aes(color=color, alpha=cut))
21 / 33
![Page 22: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/22.jpg)
Der analoge ggPlot - Version 5
myPlot + geom_point(aes(color=color, alpha=cut))
+ coord_flip()
22 / 33
![Page 23: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/23.jpg)
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
![Page 24: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/24.jpg)
Der analoge ggPlot - Version 6
myPlot + geom_point(aes(color=color, alpha=cut))
+ coord_flip() + facet_wrap(~ cut)
24 / 33
![Page 25: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/25.jpg)
Der analoge ggPlot - Version 7
myPlot + geom_smooth(aes(color=color, alpha=cut))
+ coord_flip() + facet_wrap(~ cut)
25 / 33
![Page 26: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/26.jpg)
ggplot - Nutzliche Geometrien
26 / 33
![Page 27: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/27.jpg)
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
![Page 28: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/28.jpg)
Saulendiagramm, Beispiel
(a) aes(fill=cut), position=“stack“ (b) aes(fill=cut), position=“dodge“
28 / 33
![Page 29: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/29.jpg)
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
![Page 30: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/30.jpg)
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
![Page 31: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/31.jpg)
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
![Page 32: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/32.jpg)
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
![Page 33: Programmieren in Anwendungen - softech.informatik.uni … · Uberblick Einleitung Die Funktionen qplot und ggplot Data frame \diamonds\ Der Umgang mit qplot Der Umgang mit ggplot](https://reader030.vdocuments.pub/reader030/viewer/2022021514/5b158bbb7f8b9a8b288cc056/html5/thumbnails/33.jpg)
...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