-
Notifications
You must be signed in to change notification settings - Fork 0
/
uebung4.R
96 lines (65 loc) · 3.52 KB
/
uebung4.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
library(tidyr)
library(dplyr)
library(modelr)
library(lubridate)
library(ggplot2)
library(nycflights13)
library(ggcorrplot)
library("GGally")
dat <- read.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/4_ThreeNum.csv")
dat
ggplot(dat, aes(x=gdpPercap, y=lifeExp, size=pop)) + geom_point()
ggplot(dat, aes(x=gdpPercap, y=lifeExp, size=pop)) + geom_point() + geom_smooth()
ggplot(dat, aes(x=gdpPercap, y=lifeExp, size=pop, colour=continent)) + geom_point()
## Transformationen: sclae_*_continuous, scale_*_log10, , scale_*_sqrt
ggplot(dat, aes(x=gdpPercap, y=lifeExp, size=pop, colour=continent)) + geom_point() + scale_x_sqrt()
ggplot(dat, aes(x=gdpPercap, y=lifeExp, size=pop, colour=continent)) + geom_point() + scale_x_log10()
## facettierung: facet_grid und facet_wrap
ggplot(dat, aes(x=gdpPercap, y=lifeExp, size=pop, colour=continent)) + geom_point() + facet_grid(cols = vars(continent))
ggplot(dat, aes(x=gdpPercap, y=lifeExp, size=pop, colour=continent)) + geom_point() + facet_wrap(facets = vars(continent))
## Visuelle Details
# xlab, ylab
ggplot(dat, aes(x=gdpPercap, y=lifeExp, size=pop, colour=continent)) + geom_point() + xlab("BIP pro Kopf") + ylab("Lebenserwartung")
## Visuelle Details
## scale_*_manuel
ggplot(dat, aes(x=gdpPercap, y=lifeExp, size=pop, colour=continent)) + geom_point() + scale_color_manual(values=c("green", "red", "blue", "black", "white"))
## Themes
# theme_*(bw, light)
ggplot(dat, aes(x=gdpPercap, y=lifeExp, size=pop, colour=continent)) + geom_point() + theme_bw()
ggplot(dat, aes(x=gdpPercap, y=lifeExp, size=pop, colour=continent)) + geom_point() + theme_light()
ggplot(dat, aes(x=gdpPercap, y=lifeExp, size=pop, colour=continent)) + geom_point() + theme_dark()
## Arbeiten mit Time Series Daten
today() # Tag
now() # Tag und Uhrzeit
class(now())
ymd("2021-08-09") # übersetzen in Datumsobjekt
as.integer(ymd("2021-08-09") - ymd("2021-07-16"))
mdy("02-19-1998")
ymd_hms("2023-10-08 12:12:52", tz="CET")
timestamp("12:12:00")
## Fligths Datensatz: Datumsangabe über mehrere Spalten verteilt
# diese vereinheitlichen zu einem Datum
flights %>% select(year, month, day, hour, minute)
flights %>% mutate(timestamp=make_datetime(year, month, day, hour, minute, tz="EST")) %>% select(timestamp, carrier, flight, origin, dest)
dat <- flights %>% mutate(date=make_datetime(year, month, day)) %>% mutate(time=timestamp(hour, minute)) %>% sample_frac(0.1)
dat2 <- flights %>% mutate(timestamp=make_datetime(year, month, day, hour, minute, tz="EST")) %>% mutate(week=week(timestamp)) %>% select(timestamp, starts_with("arr"), starts_with("dep"), day) %>% sample_frac(0.05)
dat3 <- dat2 %>% filter(timestamp <= ymd("2013-01-08"))
wday(today())
week(today())
ggplot(dat2, aes(x=timestamp, y=arr_delay)) + geom_smooth() + facet_grid(cols=vars("week"))
ggplot(dat3, aes(x=timestamp, y=arr_delay)) + geom_smooth()
ggplot(dat3, aes(x=timestamp)) + geom_freqpoly(bins=5)
ggplot(dat3, aes(x=timestamp, y=arr_delay)) + geom_point() + facet_grid(cols=vars("day"))
ggplot(dat3, aes(x=timestamp, y=arr_delay)) + geom_point() + facet_grid(cols=wday(timestamp))
dat3 %>% mutate(wday=wday(timestamp, label=TRUE)) %>% ggplot(aes(x=timestamp, y=arr_delay)) + geom_line() + facet_grid(cols=vars(wday), scales="free_x")
## data frames immer zuerst in tibble konvertieren
df <- read.csv("./dataset.csv")
ggp <- ggpairs(df)
df.tib <- as_tibble(df)
df[,1,2]
class(df)
class(df[,1,2])
class(df.tib)
## plots speichern
ggsave("./plot.pdf", ggp)
## konertieren in knitr damit Code und Dokumentation gemeinsam verwendet werden können