programmieren in anwendungen - ag softech · table(status_factor) 19/55. datenvisualisierung mit...
TRANSCRIPT
![Page 1: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/1.jpg)
Programmieren in Anwendungen
Annette Bieniusa
Technische Universitat Kaiserslautern
12.06.2014
1 / 55
![Page 2: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/2.jpg)
Uberblick
Fallstudie 2: UFO-Sichtungen
Programmieren in R (Teil 2)
Datenvisualisierung mit ggplot2
Fallstudie 2: UFO-Sichtungen (Fortsetzung)
2 / 55
![Page 3: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/3.jpg)
Fallstudie 2: UFO-Sichtungen
Aus: Drew Conway, John Myles White: Machine Learning forHackers - Case Studies and Algorithms to Get You Started.
O’Reilly Media, Februar 2012.
3 / 55
![Page 4: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/4.jpg)
Daten zu UFO-Sichtungen
I Rund 60.000 Datensatze gesammelt durch das National UFOReporting Center in den letzten Jahrhunderten
I Daten zum Download:
https://github.com/johnmyleswhite/ML_for_Hackers/
tree/master/01-Introduction
I Aufbau des Datensatzes
DateOccured Zeitpunkt der BeobachtungDateReported Zeitpunkt der MeldungLocation OrtShortDescription KurzbeschreibungDuration DauerLongDescription Ausfuhrliche Beschreibung
4 / 55
![Page 5: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/5.jpg)
Import des Datensatzes
I Eintrage sind durch Tabulator voneinander getrennt (.tsv -Datei)
I Datentypen (Zahlen, Strings, Faktoren, etc.) werdenheuristisch ermittelt
I Hier ist die Umwandlung von Strings in Faktoren unerwunscht
I Leerer String soll durch NAersetzt werden
ufo <- read.delim(file="ufo_awesome.tsv",sep="\t",
na.strings="", header=FALSE,
stringsAsFactors=FALSE)
5 / 55
![Page 6: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/6.jpg)
Struktur von Datensatzen
I Mit der Funktion str() erhalt man einen Uberblick zurStruktur eines Datensatzes.
> str(ufo)
’data.frame’: 61870 obs. of 6 variables:
$ V1: chr "19951009" "19951010" "19950101" "19950510" ...
$ V2: chr "19951009" "19951011" "19950103" "19950510" ...
$ V3: chr " Iowa City, IA" " Milwaukee, WI" " Shelton, WA" ...
$ V4: chr NA NA NA NA ...
$ V5: chr NA "2 min." NA "2 min." ...
$ V6: chr "Man repts. witnessing flash ...
6 / 55
![Page 7: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/7.jpg)
Schritt 1: Benennung der Spalten
I Benannte Spalten vereinfachen den Umgang mit den Daten,da weniger Verwechslungen und Vertippen moglich
names(ufo) <- c("DateOccurred","DateReported",
"Location","ShortDescr", "Duration","LongDescr")
7 / 55
![Page 8: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/8.jpg)
Schritt 2: Datumskonvertierung
I Datumsformat scheint ”YYYYMMDD”(year-month-day)
> ufo$DateOccurred <- as.Date(ufo$DateOccurred,
format="%Y%m%d")
Fehler in strptime(x, format, tz = "GMT") :
Eingabe-Zeichenkette ist zu lang
I Offensichtlich haben einige Eingaben das falsche Format!
I Ausweg: Ausfiltern der fehlerhaften Datensatze oderAusbessern von Hand mit fix() (haufig nicht moglich)
good.rows <- ifelse(nchar(ufo$DateOccurred) == 8 &
nchar(ufo$DateReported) == 8,
TRUE,FALSE)
ufo_clean <- ufo[good.row,]
I Dann Datumseintrage DateOccurred und DateReported
konvertieren mit as.Date(), wie oben
8 / 55
![Page 9: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/9.jpg)
Filtern von Datensatzen
I Einfache Filterkriterien== gleich! = ungleich>,>= großer (gleich)<,<= kleiner (gleich)
I Bei kategoriellen Daten durch %in%
haar %in% c("blond","braun")
I Kombination von Filterkriterien& Und| Oder! Negation
9 / 55
![Page 10: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/10.jpg)
Schritt 3: Bearbeitung der Ortsinformation
I Ortsformat ist scheinbar “Stadt, Staat”
get.location <- function(l) {
split.location <- tryCatch(strsplit(l, ",")[[1]],
error = function(e) return(c(NA, NA)))
clean.location <- gsub("^ ","",split.location)
if (length(clean.location) > 2) {
return(c(NA,NA))
} else {
return(clean.location)
}
}
I strsplit(x,",")
trennt die Zeichenkette bei Komma, das folgende [[1]]unquoted den Ergebnisvektor
I gsub("^ ","",x)
entfernt alle Leerzeichen am Anfang der Zeichenkette x
10 / 55
![Page 11: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/11.jpg)
Schritt 3: Bearbeitung der Ortsinformation
I Erstellen einer Matrix mit der Stadt-Staat-Information durchAnwenden von get.location mittels lapply() undZusammenfassen der resultierenden Liste durch do.call() undrbind()
I Hinzufugen dieser Matrix zum Datensatz
city.state <- lapply(ufo$Location, get.location)
location.matrix <- do.call(rbind, city.state)
ufo <- transform(ufo,
USCity = location.matrix[, 1],
USState = location.matrix[, 2],
stringsAsFactors = FALSE)
11 / 55
![Page 12: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/12.jpg)
Schritt 4: Filtern nach US-Staaten
I state.abb ist ein Vektor mit Abkurzungen derUS-Staatennamen
I match() liefert den Index in state.abb, der die passendeAbkurzung enthalt
I Falls keine passt, wird NA gesetzt
I Schließlich wird nach den Eintragen mit korrekten US-Staatengefiltert
ufo$USState <- state.abb[match(ufo$USState, state.abb)]
ufo.us <- subset(ufo, !is.na(USState))
12 / 55
![Page 13: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/13.jpg)
Ergebnis: Bearbeiteter Datensatz
> head(ufo.us)
DateOccurred DateReported Location ShortDescription Duration
1 1995-10-09 1995-10-09 Iowa City, IA <NA> <NA>
2 1995-10-10 1995-10-11 Milwaukee, WI <NA> 2 min.
3 1995-01-01 1995-01-03 Shelton, WA <NA> <NA>
4 1995-05-10 1995-05-10 Columbia, MO <NA> 2 min.
5 1995-06-11 1995-06-14 Seattle, WA <NA> <NA>
6 1995-10-25 1995-10-24 Brunswick County, ND <NA> 30
min.
LongDescription
1 Man repts. witnessing "flash, followed by a classic UFO, ...
2 Man on Hwy 43 SW of Milwaukee sees large, bright blue light ...
3 Telephoned Report:CA woman visiting daughter witness discs and ...
4 Man repts. son's bizarre sighting of small humanoid creature ...
5 Anonymous caller repts. sighting 4 ufo's in NNE sky, 45 deg. ...
6 Sheriff's office calls to rept. that deputy, 20 mi. SSE of ...
USCity USState
1 Iowa City IA
2 Milwaukee WI
3 Shelton WA
4 Columbia MO
5 Seattle WA
6 Brunswick County ND
Fortsetzung folgt...
13 / 55
![Page 14: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/14.jpg)
Programmieren in R (Teil 2)
14 / 55
![Page 15: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/15.jpg)
Eigenheiten von R
I Variablen mussen nicht deklariert werden, sondern werdendurch die erste Zuweisung verwendbar
I Indizierung startet mit 1, nicht mit 0
I Zuweisung zu einem nicht existenten Index eines Vektorserweitert den Vektor (ahnlich bei Matrix, Listen, ...)
> z <- 1:5
> z[7] <- 9
> z
[1] 1 2 3 4 5 NA 9
I Kommentare beginnen mit #
15 / 55
![Page 16: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/16.jpg)
Typische Fehlerquellen
I Verwechslung von Groß/Kleinschreibung: length() undLength() sind verschiedene Funktionen!
I Verwenden von Funktionen ohne das notwendige Packagegeladen zu haben
I Vergessen von Anfuhrungszeichen beim Laden, Installierenetc. von Packages
I Jeder Funktionsaufruf braucht Klammern, auch ohneParameter!
16 / 55
![Page 17: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/17.jpg)
Factors
I Datenwerte entstammen oft Kategorien, diese konnen eineOrdnung implizieren
I Beispiel: Geschlecht (m/w/sonstiges) [ungeordnet],Schulnoten (sehr gut, gut, befriedigend, ausreichend,mangelhaft, ungenugend) [geordnet]
I In R werden diese Datenwerte factors genannt
I Eine Ordnung kann mit spezifiziert werden, standardmassigwird die alphabetische Ordnung der Kategorien genommen
I str() gibt einen Uberblick uber die Struktur
I Intern werden die Factor-Datenwerte als Integer gespeichert,die den einzelnen Werten zugeordnet sind→ wesentlich effizienter als Strings
17 / 55
![Page 18: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/18.jpg)
Factors mit Ordnung
> noten <- c("sehr gut", "mangelhaft","gut","gut")
> noten <- factor(noten, order = TRUE)
> noten
[1] sehr gut mangelhaft gut
Levels: gut < mangelhaft < sehr gut
> noten <- factor(noten, order = TRUE,
levels=c("sehr gut","gut","mangelhaft"))
> noten
[1] sehr gut mangelhaft gut
Levels: sehr gut < gut < mangelhaft
> str(noten)
Ord.factor w/ 3 levels "sehr gut"<"gut"<..: 1 3 2
18 / 55
![Page 19: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/19.jpg)
Umkodieren von Variablen
I Beispiel: Gegeben ein Datensatz mit den Teilnehmern einerVeranstaltung
I Spalte teilnehmer$alter bezeichnet das Alter
I Gewunschte neue Variable im Datensatz:Welcher der Teilnehmer ist minderjahrig?
status <- (teilnehmer$alter < 18) * 1
+ (teilnehmer$alter >= 18) * 2
I Umwandlung von numerischem Wert in Faktor
status_factor <- factor(status,
labels = c("minderjaehrig","volljaehrig"))
I Haufigkeit der einzelnen Faktoren/Kategorien
table(status_factor)
19 / 55
![Page 20: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/20.jpg)
Datenvisualisierung mit ggplot2
20 / 55
![Page 21: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/21.jpg)
Was ist ggplot2?
I ggplot2 ist ein R-Package fur Datenvisualisierung
I Unkomplizierte, intuitive Erstellung von modernen Grafiken
I ggplot2 basiert auf der ”grammar of graphics”
I Modifikation der Plotkomponenten auf hoherAbstraktionsebene durch Layern (Schichten)
I Installation: install.packages(‘‘ggplot2’’)
I Einbindung in den Workspace: library(ggplot2)
21 / 55
![Page 22: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/22.jpg)
Anwendungsbeispiel: Datensatz 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 ...
Eigenschaften:
I carat: Gewicht des Diamanten
I cut: Qualitat des Schliffs
I color: Farbe (von J bis D) etc. (siehe help(diamonds))
22 / 55
![Page 23: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/23.jpg)
Struktur von diamonds
> str(diamonds)
’data.frame’: 53940 obs. of 10 variables:
$ carat : num 0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22...
$ cut : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 ...
$ color : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 ...
$ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5...
$ depth : num 61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1...
$ table : num 55 61 65 58 58 57 57 55 61 61 ...
$ price : int 326 326 327 334 335 336 336 337 337 338 ...
$ x : num 3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 ...
$ y : num 3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78...
$ z : num 2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49...
23 / 55
![Page 24: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/24.jpg)
Datenvisualisierung mit ggplot2
24 / 55
![Page 25: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/25.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)
Wichtigste Parameter in rot!
I data: Datensatz mit x,y,... Spalten
I geom: siehe nachste Seite
I xlim, ylim: Achsenabschnitte
I xlab, ylab: Achsenbeschriftung
I main: Uberschrift
25 / 55
![Page 26: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/26.jpg)
Einfache Geometrien
(a) qplot(data=diamonds,price) (b) qplot(data=diamonds,price,carat)
26 / 55
![Page 27: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/27.jpg)
Standardwerte fur Geometrien
Eindimensionale Geometrien (falls nur x spezifiert ist)
I geom = “histogram” Histogramm (Standard), binwidth =
... gibt die Große des Bins an
I geom = “freqpoly” und geom=“density” Frequenz- undDichteplot
Zweidimensionale Geometrien (falls x und y spezifiert sind)
I geom = “point” Scatterplot (Standard)
I geom = “smooth” Glattung
I geom = “boxplot” Box-and-whisker Plot, liefert Informationenzur Verteilung der Daten
I geom=“path” und geom=“line” Verbindungslinien zwischenden Datenpunkten, oft genutzt fur zeitliche Entwicklungen
27 / 55
![Page 28: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/28.jpg)
Plot mit Beschriftung
qplot(data=diamonds, price, carat, main="ggPlot2!",
xlab="Preis", ylab="Karat")
28 / 55
![Page 29: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/29.jpg)
Weitere Aspekte: Farbe
qplot(data=diamonds, price, carat, color=color)
29 / 55
![Page 30: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/30.jpg)
Weitere Aspekte: Alpha-Wert
qplot(data=diamonds, price, carat, color=color,
alpha=cut)
Weitere Information: http://ggplot2.org/book/qplot.pdf30 / 55
![Page 31: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/31.jpg)
Datenvisualisierung mit ggplot2
31 / 55
![Page 32: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/32.jpg)
ggplot - Grafik fur Fortgeschrittene
I Grundanweisung: ggplot(data, aes)
I Die Aesthetik definiert, wie Datenvariablen auf visuelleEigenschaften der benutzten Geometrie ubertragen werden:aes(x, y, color, alpha, size,...)
I Eine Aesthetik ist fur alle folgenden Layer gultig
I Inkrementelle Ploterstellung durch Addition von Anweisungenmithilfe von +
32 / 55
![Page 33: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/33.jpg)
Histogramm
myPlot <- ggplot(data=diamonds, aes(x=price))
myPlot <- myPlot + geom_bar()
33 / 55
![Page 34: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/34.jpg)
Scatterplot
myPlot <- ggplot(data=diamonds, aes(x=price, y=carat))
myPlot + geom_point() + ggtitle("ggPlot2")
+ xlab("Preis") + ylab("Karat")
34 / 55
![Page 35: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/35.jpg)
Scatterplot mit Farbe
myPlot + geom_point(aes(color=color))
35 / 55
![Page 36: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/36.jpg)
Scatterplot mit Farbe und Alphawert
myPlot + geom_point(aes(color=color, alpha=cut))
36 / 55
![Page 37: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/37.jpg)
Wechsel der Koordinaten
myPlot + geom_point(aes(color=color, alpha=cut))
+ coord_flip()
37 / 55
![Page 38: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/38.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
38 / 55
![Page 39: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/39.jpg)
Aufteilung mit Facette cut
myPlot + geom_point(aes(color=color, alpha=cut))
+ coord_flip() + facet_wrap(~ cut)
39 / 55
![Page 40: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/40.jpg)
Glattung zur Visualisierung von Trends
myPlot + geom_smooth(aes(color=color, alpha=cut))
+ coord_flip() + facet_wrap(~ cut)
40 / 55
![Page 41: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/41.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
)
41 / 55
![Page 42: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/42.jpg)
Beispiel: Saulendiagramm
(c) aes(fill=cut), position=“stack“ (d) aes(fill=cut), position=“dodge“
42 / 55
![Page 43: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/43.jpg)
Kreisdiagramme als spezielle Saulendiagramme+ geom bar() + coord polar(
theta=“x“ #welche Achse bildet den Kreisumfang, “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
(e) theta=“x“
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
(f) theta=“y“
43 / 55
![Page 44: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/44.jpg)
Frequency polygon
geom freqpoly(
data=w, #Definition eigener Data frame
aes(group=x,color=x),
#Linien nach Kriterium x, Farben der Kriterien
position=“z“ #Anordnung der Linien
)
44 / 55
![Page 45: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/45.jpg)
Beispiel: Frequency polygon
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
45 / 55
![Page 46: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/46.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
46 / 55
![Page 47: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/47.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
(g) geom violin() (h) geom polygon()
Weitere Ideen und Moglichkeiten:http://docs.ggplot2.org/current/
47 / 55
![Page 48: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/48.jpg)
Fallstudie 2: UFO-Sichtungen (Fortsetzung)
48 / 55
![Page 49: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/49.jpg)
Visualisierung der Daten
I Ziel: Zeitlicher Verlauf der Sichtungen pro US-Staat
I Beschrankung auf die Zeit nach 1990, da davor nursporadische Sichtungen
> summary(ufo.us$DateOccurred)
Min. 1st Qu. Median Mean ...
"1400-06-30" "1999-09-07" "2004-01-10" "2001-02-13" ...
ufo.us <- subset(ufo.us, DateOccurred >= as.Date("1990-01-01"))
49 / 55
![Page 50: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/50.jpg)
Schritt 5: Aggregieren nach Monat und JahrI Hinzufugen eines weiteren Aspekts, namlich Jahr und Monat
der BeobachtungI strftime() formatiert das Datum nach dem angegebenen
MusterI ddply() nimmt den Datensatz, splittet ihn in die verschiedenen
Untergruppen (hier: Staat und Jahr-Monat) und wendet dieFunktion nrow auf die Untergruppen an
I Der resultierende Vektor ist geordnet nach Staaten(alphabetisch) und Datum (chronologisch)
> ufo.us$YearMonth <- strftime(ufo.us$DateOccurred, format = "%Y-%m")
> sightings.counts <- ddply(ufo.us, .(USState,YearMonth), nrow)
> head(sightings.counts)
USState YearMonth V1
1 AK 1936-10 1
2 AK 1943-04 1
3 AK 1949-06 1
4 AK 1954-02 1
5 AK 1955-06 1
50 / 55
![Page 51: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/51.jpg)
Schritt 6: Erganzung fehlender Eintrage
I Erstellen eines (geordneten) Vektors mit allenJahr-Monat-Variationen und Konvertierung in passendesStringformat
I Kombinieren mit den US-Staaten
I Zusammenfassen der resultierenden Liste zu einem Vektor
date.range <- seq.Date(from = as.Date(min(ufo.us$DateOccurred)),
to = as.Date(max(ufo.us$DateOccurred)),
by = "month")
date.strings <- strftime(date.range, "%Y-%m")
states.dates <- lapply(state.abb, function(s) cbind(s, date.strings))
states.dates <- data.frame(do.call(rbind, states.dates),
stringsAsFactors = FALSE)
51 / 55
![Page 52: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/52.jpg)
Schritt 6: Erganzung fehlender Eintrage
I Zusammenfugen der beiden (geordneten) Datensatze
I all=TRUE bewirkt, dass fehlende Eintrage in sightings.countsmit NA erganzt werden
all.sightings <- merge(states.dates,
sightings.counts,
by.x = c("s", "date.strings"),
by.y = c("USState", "YearMonth"),
all = TRUE)
52 / 55
![Page 53: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/53.jpg)
Schritt 7: Konvertieren der Datentypen
I Benennung der Spalten
I Ersetzen von NA durch 0
I Umwandeln der Datum-Strings in Datum-Objekte
I Umwandeln der Staatenkurzel in Factors
names(all.sightings) <- c("State", "YearMonth", "Sightings")
all.sightings$Sightings[is.na(all.sightings$Sightings)] <- 0
all.sightings$YearMonth <- as.Date(rep(date.range, length(state.abb)))
all.sightings$State <- as.factor(all.sightings$State)
53 / 55
![Page 54: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/54.jpg)
Schritt 8: Generieren der Graphik
I facet wrap() liefert Plots fur jeden Staat in einem 10x5 Raster
I theme bw() andert den Standard-Style zu einem Style mitweißem Hintergrund
I scale color manual() setzt dunkelblau als Linienfarbe
I scale x date() rastert die x-Achse als Datumformat imAbstand von 5 Jahren
state.plot <-
ggplot(all.sightings, aes(x=YearMonth,y=Sightings)) +
geom_line(aes(color="darkblue")) +
facet_wrap(~State, nrow=10, ncol=5) +
theme_bw() +
scale_color_manual(values=c("darkblue"="darkblue"),guide="none") +
scale_x_date(breaks="5 years", labels=date_format(’%Y’)) +
xlab("Years") +
ylab("Number of Sightings") +
ggtitle("Number of UFO sightings by U.S. State (1990-2010)")
54 / 55
![Page 55: Programmieren in Anwendungen - AG Softech · table(status_factor) 19/55. Datenvisualisierung mit ggplot2 20/55. Was ist ggplot2? I ggplot2 ist ein R-Package f ur Datenvisualisierung](https://reader030.vdocuments.mx/reader030/viewer/2022040708/5e0b84d65271ef36c145e191/html5/thumbnails/55.jpg)
UFO-Sichtungen in den einzelnen US-Staaten (1990-2010)
AK AL AR AZ CA
CO CT DE FL GA
HI IA ID IL IN
KS KY LA MA MD
ME MI MN MO MS
MT NC ND NE NH
NJ NM NV NY OH
OK OR PA RI SC
SD TN TX UT VA
VT WA WI WV WY
0.0e+005.0e−061.0e−051.5e−05
0.0e+005.0e−061.0e−051.5e−05
0.0e+005.0e−061.0e−051.5e−05
0.0e+005.0e−061.0e−051.5e−05
0.0e+005.0e−061.0e−051.5e−05
0.0e+005.0e−061.0e−051.5e−05
0.0e+005.0e−061.0e−051.5e−05
0.0e+005.0e−061.0e−051.5e−05
0.0e+005.0e−061.0e−051.5e−05
0.0e+005.0e−061.0e−051.5e−05
1993 1998 2003 2008 1993 1998 2003 2008 1993 1998 2003 2008 1993 1998 2003 2008 1993 1998 2003 2008Years
Per
Cap
ita N
umbe
r of
Sig
htin
gs (
2000
Cen
sus)
Number of UFO sightings by Month−Year and U.S. State (1990−2010)
55 / 55