-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.R
209 lines (182 loc) · 6.73 KB
/
server.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
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
library(shiny)
library(tidyverse)
library(plotly)
# Data pre-processing -----------------------------------------------------
# Read data from file
raw <- readRDS("Data/atl_2015.RDS") %>%
rename(ethnic = ethnic.origin,
job = job.title,
salary = annual.salary) %>%
mutate(gender = factor(sex),
ethnic = factor(ethnic)) %>%
as_tibble()
# A count helper
which_less_than <- function(vec, n) {
x <- table(vec) < n
sort(unique(vec))[x]
}
# Id age group that has a sample size smaller than 10
sub_age <- with(raw, age %>% which_less_than(10))
# Id ethnic group that has a sample size smaller than 30
sub_ethnic <- with(raw, ethnic %>% which_less_than(30))
# Filter small sample size group for both 'age' and 'ethnic'
dat <- raw %>%
filter(!(age %in% sub_age), !(ethnic %in% sub_ethnic)) %>%
# and simplify ethnic group to first word only
mutate(ethnic = factor(stringr::str_extract(ethnic, "^[A-Z]?[a-z]+")))
# Server functions -------------------------------------------------------------
# Explore 'gender', "age", or 'ethnic' variables on 'salary'
shinyServer(function(input, output, session) {
# Set ggplot2 theme
old <- theme_set(theme_light() +
theme(plot.title = element_text(size = 11),
legend.position = "none",
text = element_text(family = "Menlo")))
# Selector window (Age + Gender / Ethnic)
output$selector <- renderPlot({
dat %>%
ggplot(aes(age, salary)) +
# enable toggle of input button
aes_string(col = input$col) +
geom_point(position = position_jitter(width = 0.3, height = 0.1)) +
scale_y_continuous(labels = scales::dollar,
# a little buffer to make it looks nicer
limits = c(min(dat$salary), max(dat$salary) + 5e4)) +
labs(x = "Age",
y = "Salary (USD)",
col = toupper(as.character(input$col))) +
# retrieve legend for this plot only
theme(
legend.position = c(0.1, 0.8),
legend.title = element_text(size = 18),
legend.text = element_text(size = 15),
# legend.key = element_rect(colour = 'gray', linetype = 'dashed'),
legend.key.width = unit(1, "cm")
)
})
# User selected points
brushPts <- reactive({
# when none selected, return all rows
if(is.null(input$selector_brush)) {
return(dat)
}
# using Shiny default brush selector
subs <- brushedPoints(dat, input$selector_brush)
# defensive: make sure user selects some mininum points
if(nrow(subs) > 10) {
subs
} else{
dat
}
})
# Find median salary by age
# to pass to plot 1
grp_by_age <- reactive({
brushPts() %>%
group_by(gender, age) %>%
summarise(salary = median(salary)) %>%
ungroup()
})
# Find median salary by ethnic
# to pass to plot 2
grp_by_ethnic <- reactive({
brushPts() %>%
group_by(gender, ethnic) %>%
summarise(salary = median(salary)) %>%
ungroup()
})
# So that our plots can share a common y-axis
axisy <- reactive({
# get range of y-axis value
range_y <- range(grp_by_age()$salary, grp_by_ethnic()$salary)
scale_y_continuous(labels = scales::dollar,
limits = range_y,
position = "left")
})
# Plot 1 - lineplot (Gender + Age)
output$age <- renderPlotly({
p <- grp_by_age() %>%
ggplot(aes(
age,
salary,
col = gender,
group = gender,
# custom tooltip
text = paste("Gender:", gender,
"\nAge:", age,
"\nSalary:", salary)
)) +
geom_line() +
geom_rug(sides = "l") +
axisy() +
labs(
x = "",
y = "",
col = "Gender",
title = "Plot I: Salary by Age Group"
)
# convert to interactive
ggplotly(p, tooltip = c("text"))
})
# Get ethnic group salary range for geom_linerange
linerange <- reactive({
## defence against too few samples
if(length(unique(grp_by_ethnic()$gender)) < 2){
return(NULL)
}
# First we compute the median salary
# and determine the range
brushPts() %>%
group_by(gender, ethnic) %>%
summarise(salary = median(salary)) %>%
# convert to long format so that we can do comparison
tidyr::spread(gender, salary, fill = 0) %>%
# pick maximum and maximum rowwise (female vs male)
rowwise() %>%
mutate(ymax = max(Female, Male),
ymin = min(Female, Male))
})
# Plot 2 - dumbbell plot (Gender + Ethnic)
output$ethnic <- renderPlotly({
## defensive: If only single gender is selected
if (length(unique(grp_by_ethnic()$gender)) < 2) {
# return a base object, not plotting anything meaningful
p <- ggplot(aes(ethnic, salary), data = brushPts())
return(ggplotly(p))
}
p <- grp_by_ethnic() %>%
ggplot(aes(
ethnic,
salary,
col = gender,
text = paste("Gender:", gender,
"\nEthnic:", ethnic,
"\nSalary:", salary)
)) +
geom_linerange(
aes(x = ethnic, ymin = ymin, ymax = ymax),
# a reactive func passed down from above
data = linerange(),
inherit.aes = FALSE,
col = "darkgrey"
) +
# It is important to put geom_point last so that it overlaps the lines
geom_point(size = 3) +
axisy() +
labs(
x = "",
y = "",
col = "Gender",
title = "Plot II: Salary By Ethnic Group"
) +
# Hide this y-axis, we have one on the left already
theme(axis.text.y = element_blank())
ggplotly(p, tooltip = c("text"))
})
# Summary table
output$summ <- renderPrint({
# a simple contingency table
with(brushPts(), table("Gender" = gender, "Ethnic" = ethnic))
})
### The End ###
})