-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathivi_notebook.Rmd
221 lines (197 loc) · 5.79 KB
/
ivi_notebook.Rmd
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
---
title: "IVI Notebook"
output: html_notebook
---
#Setup
```{r}
#install.packages("tidyverse")
#install.packages("PerformanceAnalytics")
#install.packages("ggfortify")
#install.packages("fastDummies")
library(tidyverse) # core package includes following packages: tidyr, dplyr, ggplot2, readr, purrr, tibble, stringr, forcats
library(plotly)
library("PerformanceAnalytics") #for correlation
library(broom) # for model quantification
library(ggfortify) # for visualizing model fits
library(fastDummies)
library(dplyr)
library(ggplot2)
```
```{r}
library(tidyverse)
library(dplyr)
library(tidyr)
library(ggplot2)
library(magrittr)
#install.packages("PerformanceAnalytics")
library("PerformanceAnalytics")
#install.packages("scales")
#library(scales)
```
### Data Wrangling
```{r}
bike_data <- read_csv("SeoulBikeData.csv",
show_col_types = FALSE,
col_types = cols(Date = col_date(format = "%d/%m/%Y"),
Seasons = col_factor(levels = c("Winter", "Spring", "Summer", "Autumn"),
ordered = TRUE),
Holiday = col_factor(),
"Functioning Day" = col_factor()
))
```
```{r}
bike_data <- bike_data %>%
mutate(day = weekdays(Date),
month = months(Date),
day_time = case_when(
Hour >= 5 & Hour < 11 ~ "Morning",
Hour >= 11 & Hour < 15 ~ "Noon",
Hour >= 15 & Hour < 18 ~ "Afternoon",
Hour >= 18 & Hour < 22 ~ "Evening",
Hour < 5 | Hour >= 22 ~ "Night")) %>%
select(Date,month,day,Hour,day_time, Holiday, 'Rented Bike Count',everything())
```
```{r}
bike_data$day_time <- factor(bike_data$day_time,
levels = c("Morning", "Noon", "Afternoon", "Evening", "Night"),ordered = TRUE)
bike_data$day <- factor(bike_data$day,
levels = c("Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag" ))
bike_data$month <- factor(bike_data$month,
levels = c("Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober","November", "Dezember"))
```
#LO1: Performance
## Grafik 1
```{r}
grafik_1 <- bike_data %>%
plot_ly(x = ~Seasons) %>%
add_histogram(color = I("darkgreen"), opacity = 0.9) %>%
layout(title = "Total bike count by seasons")
```
## Zeit messen bei Grafik 1
```{r}
start <- Sys.time()
grafik_1
end <- Sys.time()
print(end - start)
```
## Grafik 2
```{r}
grafik_2 <- ggplot(bike_data, aes(`Temperature` ,`Rented Bike Count`, color = `Seasons`))+
geom_jitter(alpha = 0.3)+
scale_fill_grey(start = 0.2, end = 0.8,na.value = "red")+
stat_smooth(method = lm, se = FALSE, color = "red")+
labs(
x = "Temperature in Celsius",
y = "Rented Bikes",
title = "Correlation between temperature and rented bikes")+
theme_minimal()
```
## Zeit messen bei Grafik 2
```{r}
start <- Sys.time()
grafik_2
end <- Sys.time()
print(end - start)
```
#LO2: Dashboard design principes
## Grafik 3:
```{r}
grafik_3 <- bike_data %>%
plot_ly(x = ~Seasons) %>%
add_histogram(color = I("navy"), opacity = 0.9) %>%
layout(title = "Total bike count by seasons")
grafik_3
```
##Grafik 4:
```{r}
grafik_4 <- ggplot(bike_data, aes(`Temperature` ,`Rented Bike Count`, color = `Seasons`, colors = "Dark2"))+
geom_jitter(alpha = 0.3)+
scale_fill_grey(start = 0.2, end = 0.8,na.value = "red")+
stat_smooth(method = lm, se = FALSE, color = "black")+
labs(
x = "Temperature in Celsius",
y = "Rented Bikes",
title = "Correlation between temperature and rented bikes")+
theme_minimal()
```
```{r}
ggplotly(grafik_4)
```
##Grafik 5
```{r}
grafik_5 <- ggplot(bike_data,aes(x= Snowfall, y = `Rented Bike Count`))+
geom_jitter(shape=8, (aes(color = Temperature)))+
scale_color_gradient(low="dark blue", high= "light blue")+
facet_wrap(~month)+
xlab("Schnee in Zentimetern") +
ylab("Ausgeliehene Fahrräder")+
ggtitle("Anzahl ausgeliehene Fahrräder und der Einfluss von Schnee")+
theme_minimal()
grafik_5
```
```{r}
ggplotly(grafik_5)
```
## Zoomen und Slider: Schlechtes Beispiel
```{r}
grafik_6 <- ggplot(bike_data, aes(Hour, `Rented Bike Count`))+
geom_point(aes(color = day), alpha = 0.5) +
geom_smooth(aes(color = day, fill = day), method = "lm")+
xlab("Uhrzeit") +
ylab("Anzahl ausgeliehener Fahrräder")+
theme_minimal()
grafik_6
```
```{r}
ggplotly(grafik_6, dynamicTicks = TRUE) %>%
rangeslider() %>%
layout(hovermode = "x")
```
## Zoomen und Slider: Gutes Beispiel
```{r}
# Schritt 1: Übersichtliche Grafik machen mit Stunde und ausgeliehenen Fahrrädern nach Wochentag aufgeteilt.
library(RColorBrewer)
display.brewer.all(colorblindFriendly = TRUE)
grafik_8 <- bike_data %>%
ggplot(aes(Hour, `Rented Bike Count`, color = day)) +
geom_smooth(se = F, size = 2) +
xlab("Uhrzeit") +
ylab("Anzahl ausgeliehener Fahrräder")+
scale_color_discrete("")+
scale_color_brewer(palette = "Paired")+
theme_minimal()
grafik_8
```
```{r}
#Schritt 2: Den Slider hinzufügen
ggplotly(grafik_8, dynamicTicks = TRUE) %>%
rangeslider() %>%
layout(hovermode = "x", title = "Der Trend von Fahrradausleihen zu verschiedenen Zeiten und an Wochentagen")
```
#LO3: HCl basics
```{r}
# Einfacher Linienplot zum Regen in Seoul
ggplot(bike_data) +
geom_line(aes(x=Date,y=Snowfall),color="deepskyblue")+
geom_line(aes(x=Date, y=Rainfall), color="navy")+
labs(title="Täglich gemessener Schneefall und Regen über ein Jahr")+
xlab(label = "Datum")+ ylab(label = " ")+
scale_x_date(date_breaks="months",date_labels="%Y-%m")+
theme_minimal()
rslocator <- function(n=512, type="p", ...)
{
on.exit(return(list(x=x,y=y))) # output even when function is canceled with ESC in console
x <- y <- NULL
i <- 1
while(i<=n)
{
d <- locator(1)
if(is.null(d)) break # If user pressed ESC in Rstudio Graphics window
x <- c(x, d$x)
y <- c(y, d$y)
points(x,y, type=type, ...)
i <- i+1
}
}
```
#LO4: Evaluation