forked from STAT547-UBC-2019-20/deployed_class_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash_functions.R
71 lines (55 loc) · 2.09 KB
/
dash_functions.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
suppressPackageStartupMessages(library(plotly))
suppressPackageStartupMessages(library(tidyverse))
library(gapminder)
# Storing the labels/values as a tibble means we can use this both
# to create the dropdown and convert colnames -> labels when plotting
yaxisKey <- tibble(label = c("GDP Per Capita", "Life Expectancy", "Population"),
value = c("gdpPercap", "lifeExp", "pop"))
## Make plot
make_plot <- function(yaxis = "gdpPercap",
scale = "linear"){
# gets the label matching the column value
y_label <- yaxisKey$label[yaxisKey$value==yaxis]
#filter our data based on the year/continent selections
data <- gapminder
# make the plot!
### the customdata mapping adds country to the tooltip and allows
# its selection using clickData.
p <- ggplot(data, aes(x = year, y = !!sym(yaxis),
colour = continent,
customdata=country)) +
geom_jitter(alpha=0.6) +
scale_color_manual(name = 'Continent', values = continent_colors) +
scale_x_continuous(breaks = unique(data$year))+
xlab("Year") +
ylab(y_label) +
ggtitle(paste0("Change in ", y_label, " over time (Scale : ", scale, ")")) +
theme_bw()
if (scale == 'log'){
p <- p + scale_y_continuous(trans='log10')
}
ggplotly(p) %>%
### this is optional but changes how the graph appears on click
# more layout stuff: https://plotly-r.com/improving-ggplotly.html
layout(clickmode = 'event+select')
}
### Create the line graph
make_country_graph <- function(country_select="Canada",
yaxis="gdpPercap"){
# gets the label matching the column value
y_label <- yaxisKey$label[yaxisKey$value==yaxis]
#filter our data based on the year/continent selections
data <- gapminder %>%
filter(country == country_select)
# make the plot
p <- ggplot(data, aes(x=year, y=!!sym(yaxis), colour=continent)) +
geom_line() +
scale_color_manual(name="Continent", values=continent_colors) +
scale_x_continuous(breaks = unique(data$year))+
xlab("Year") +
ylab(y_label) +
ggtitle(paste0("Change in ", y_label, " Over Time: ",
country_select)) +
theme_bw()
ggplotly(p)
}