-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive-dashboards-in-r.Rmd
157 lines (109 loc) · 4.56 KB
/
interactive-dashboards-in-r.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
---
title: "Dashboard"
knit: (function(input_file, encoding) {
out_dir <- 'docs';
rmarkdown::render(input_file,
encoding=encoding,
output_file=file.path(dirname(input_file), out_dir, 'index.html'))})
output:
flexdashboard::flex_dashboard:
vertical_layout: scroll
theme: cosmo
---
The current code is taken from Kaggle kernel [Interactive Dashboards in R](https://www.kaggle.com/philippsp/interactive-dashboards-in-r) to set up a Dashboard in R using [flexdashboard](http://rmarkdown.rstudio.com/flexdashboard/layouts.html), to demonstrate how flexdashboard can be hosted on github page. Furthermore it uses [highcharter](http://jkunst.com/highcharter/) to create interactive plots.
Credit for the dashboard code: [Philipp Spachtholz](https://www.kaggle.com/philippsp)
```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(readr)
library(highcharter)
library(dplyr)
train <- read_csv('./input/train.csv')
train$Pclass <- factor(train$Pclass, labels = c("1st", "2nd", "3rd"))
train$Embarked <- factor(train$Embarked, labels = c("Cherbourg", "Queenstown", "Southhampton"))
```
Sample Layout 1 {data-orientation=rows data-icon="fa-bar-chart"}
=====================================
## Row 1 {data-height=110}
### Passengers of the Titanic
```{r}
valueBox(2222, icon = "fa-ship", color="rgb(100,100,100)")
```
### Percentage of survivors
```{r}
valueBox("31.6 %", icon = "fa-heart", color="rgb(200,100,100)")
```
### Water temperature
```{r}
valueBox("-2°C", icon = "fa-life-ring",color="rgb(26,110,204)")
```
## Row 2 {data-height=400}
### Age
```{r}
tmp_male <- train %>% filter(Sex=="male", !is.na(Age)) %>% select(Age) %>% .[[1]]
b <- hist(tmp_male, 20, plot=FALSE)
tmp_female <- train %>% filter(Sex=="female", !is.na(Age)) %>% select(Age) %>% .[[1]]
a <- hist(tmp_female, breaks = b$breaks, plot=FALSE)
df <- data.frame(Age=c(a$mids,b$mids),Density=c(a$density,b$density),Sex=c(rep("female",length(a$mids)),rep("male",length(b$mids))))
highchart() %>%
hc_add_series(name="female", select(filter(df,Sex=="female"),Density)[[1]], type="column", color='rgba(255, 192, 203, 0.30)', showInLegend=FALSE) %>%
hc_add_series(name="male", select(filter(df,Sex=="male"),Density)[[1]], type="column", color='rgba(68, 170, 255, 0.30)', showInLegend=FALSE) %>%
hc_add_series(name="male", select(filter(df,Sex=="male"),Density)[[1]], type="spline", color="#44AAFF") %>%
hc_add_series(name="female", select(filter(df,Sex=="female"),Density)[[1]], type="spline", color="#FFC0Cb") %>%
hc_tooltip(pointFormat = "<span style=\"color:{series.color}\">{series.name}</span>:
{point.y:.3f}<br/>",
shared = FALSE) %>%
hc_yAxis(title=list(text='Density')) %>%
hc_xAxis(title=list(text='Age'))
```
## Row 3 {data-height=400}
### Port of Embarkation
```{r}
tmp <- train %>% filter(!(Embarked=="")) %>% group_by(Embarked) %>% tally() %>% mutate(Percent = n/sum(n))
tmp$colors <- c("#d35400", "#2980b9", "#2ecc71")
tmp <- arrange(tmp,desc(Percent))
highchart() %>%
hc_xAxis(categories = c("Southhampton", "Cherbourg", "Queenstown")) %>%
hc_yAxis(title=list(text='Percentage')) %>%
hc_add_series(tmp, "bar", hcaes(x = Embarked, y = Percent, color=colors)) %>%
hc_tooltip(pointFormat = "{point.y:.2f}</br>",shared = FALSE) %>%
hc_legend(enabled=FALSE)
```
### Passenger Class
```{r}
tmp <- train %>% group_by(Pclass) %>% summarize(Survived = mean(Survived))
tmp$colors <- c("#d35400", "#2980b9", "#2ecc71")
hchart(tmp, "column", hcaes(x = Pclass, y = Survived, color=colors)) %>%
hc_tooltip(pointFormat = "{point.y:.2f}</br>",shared = FALSE)
```
Sample Layout 2 {data-icon="fa-area-chart"}
=====================================
Column {data-width=450}
-------------------------------------
### Kernel Finished
```{r}
rate <- 95
gauge(rate, min = 0, max = 100, symbol = '%', gaugeSectors(
success = c(80, 100), warning = c(40, 79), danger = c(0, 39)
))
```
### Sex
```{r}
tmp <- train %>% group_by(Sex) %>% tally() %>% mutate(pct = n/sum(n))
tmp$colors <- c("#d35400", "#2980b9")
hchart(tmp, "pie", hcaes(x = Sex, y = pct, color=colors))
```
Column {data-width=450}
-------------------------------------
### Number of votes
```{r}
gauge(59, min = 0, max = 75, gaugeSectors(
success = c(65, 75), warning = c(30, 64), danger = c(0, 29)
))
```
### Port of Embarkation
```{r}
tmp <- train %>% filter(!(Embarked=="")) %>% group_by(Embarked) %>% summarize(Survived = mean(Survived))
tmp$colors <- c("#d35400", "#2980b9", "#2ecc71")
hchart(tmp, "column", hcaes(x = Embarked, y = Survived, color=colors))
```