forked from UBC-MDS/DSCI532_Group112_Unemployment_DashR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash_plots.Rmd
128 lines (99 loc) · 3.91 KB
/
dash_plots.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
---
title: "dash_plots"
author: "Braden Tam"
date: "07/12/2019"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
library(scales)
unemply_df_year <- read_csv("data/unemply_df_year.csv")
unemply_df_month <- read_csv("data/unemply_df_month.csv")
df <- unemply_df_year %>%
select(-c(count, rate, X1))
df <- df %>%
spread(key = 'year', value = 'total')
# plot 1
make_plot_1 <- function(year_range = c(2000, 2003), stat = "rate"){
new_df <- df %>%
select(industry)
if(stat == "rate"){
new_df$rate <- unlist(round((df[as.character(year_range[2])] -
df[as.character(year_range[1])]) / df[as.character(year_range[1])], 2))
new_df <- new_df %>%
mutate(colour = ifelse(rate < 0, "type1", "type2"))
ggplot(new_df, aes(industry, rate, colour = colour)) +
geom_segment(aes(xend = industry, y = 0, yend = rate)) +
geom_point(size = 2) +
coord_flip() +
scale_y_continuous(labels = percent_format(accuracy = 1L)) +
theme(legend.position = "none") +
labs(x = '', y = 'Percentage Change')
} else {
new_df$count <- unlist(round((df[as.character(year_range[2])] - df[as.character(year_range[1])])))
new_df <- new_df %>%
mutate(colour = ifelse(count < 0, "type1", "type2"))
ggplot(new_df, aes(industry, count, colour = colour)) +
geom_segment(aes(xend = industry, y = 0, yend = count)) +
geom_point(size = 2) +
coord_flip() +
theme(legend.position = "none") +
labs(x = ' ', y = 'Absolute Change')
}
}
# plot 2
make_plot_2 <- function(industries = c("Agriculture", "Construction"), stat = "rate"){
new_df <- unemply_df_year %>%
filter(industry %in% industries)
if(stat == "rate"){
ggplot(new_df, aes(factor(year), rate, colour = industry, group = industry)) +
geom_line() +
geom_point() +
scale_y_continuous(labels = percent_format(accuracy = 1L)) +
labs(x = '', y = 'Rate', colour = 'Industry')
} else {
ggplot(new_df, aes(factor(year), count, colour = industry, group = industry)) +
geom_line() +
geom_point() +
labs(x = '', y = 'Count', colour = 'Industry')
}
}
# plot 3
make_plot_3 <- function(industries = c("Agriculture", "Construction"), year_desired = 2000, stat = "rate"){
avg_df <- unemply_df_month %>%
group_by(month) %>%
summarize(rate = mean(rate),
count = mean(count))
new_df <- unemply_df_month %>%
filter(year == year_desired,
industry %in% industries)
if(stat == "rate"){
ggplot(new_df, aes(factor(month), rate, colour = industry, group = industry)) +
geom_line() +
geom_point() +
scale_y_continuous(labels = percent_format(accuracy = 1L)) +
scale_x_discrete(breaks = seq_along(1:12), labels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) +
labs(x = '', y = 'Rate', colour = 'Industry')
} else {
ggplot(new_df, aes(factor(month), count, colour = industry, group = industry)) +
geom_line() +
geom_point() +
scale_x_discrete(breaks = seq_along(1:12), labels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) +
labs(x = '', y = 'Count', colour = 'Industry')
}
}
# ggplot() +
# geom_line(new_df, mapping = aes(factor(month), rate, colour = industry, group = industry)) +
# geom_point(new_df, mapping = aes(factor(month), rate, colour = industry, group = industry)) +
# geom_line(avg_df, mapping = aes(month, rate), alpha = 0.5, linetype = 'dashed') +
# labs(x = '', y = 'Unemployment Rate', colour = 'Industry')
```
```{r, echo = FALSE, fig.height=7, fig.width=12}
library(cowplot)
plot_grid(make_plot_2(), make_plot_3(), make_plot_1())
```