-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
242 lines (215 loc) · 9.87 KB
/
app.R
File metadata and controls
242 lines (215 loc) · 9.87 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# App developed my Data Cube Solutions
# contactdatacube@gmail.com / molo.andrew@gmail.com
# Data is reproducible
# https://stackoverflow.com/questions/54914541/global-r-dont-start/66802176#66802176
library(easypackages)
libraries("shiny","shinydashboard","tidyverse","lubridate", "plotly","Rcpp","shinyjs","rsconnect")
# theme_set(theme_minimal())
# Visuals ####
# Dataset
medicalPractitioners <- read.csv("Data/MedicalPractitioners.csv")
medicalPractitioners$RegDate <- dmy(medicalPractitioners$RegDate)
medicalPractitioners <- medicalPractitioners %>%
select(RegDate, SPECIALTY, SUB_SPECIALTY, Qualification.Count, TOWN) %>%
rename(
`Registration Date` = RegDate,
Specialty = SPECIALTY,
`Sub Specialty` = SUB_SPECIALTY,
`Number of Qualifications` = Qualification.Count,
Town = TOWN) %>%
dplyr::mutate(Year = lubridate::year(`Registration Date`))
medicalPractitioners$`Year Range` = cut(medicalPractitioners$Year, c(1970, 1980, 1990, 2000, 2010, 2020, 2025))
levels(medicalPractitioners$`Year Range`) = c("1970-1980", "1981-1990", "1991-2000","2001-2010", "2011-2020","2021")
medicalPractitioners$`Year Range` <- factor(medicalPractitioners$`Year Range`, ordered = T, levels = c("1970-1980", "1981-1990", "1991-2000","2001-2010", "2011-2020"))
# Function for converting to Factors
to_factor <- c("Specialty","Sub Specialty","Number of Qualifications","Town")
for (col in to_factor) {
medicalPractitioners[[col]] <- as.factor(as.character(medicalPractitioners[[col]]))
}
## Plots ####
## Count of Medical Practitioners ####
medicalPractitioners %>%
group_by(`Year Range`) %>%
summarise(count = n()) %>%
ggplot(aes(`Year Range`, count)) +
geom_line(aes(group = 1),color="#aa2b1d", size=1) +
geom_point(size=4, color="#28527a") +
labs(title = "Count of Medical Practitioners in Kenya",
subtitle = "Data from 1978 to 2020",
caption = "Source: medicalboard.co.ke",
x="") +
theme(
plot.title = element_text(color = "#23689b", size = 20, face = "bold",hjust = 0.5),
plot.subtitle = element_text(color = "#161d6f", size = 13, face = "bold",hjust = 0.5),
plot.caption = element_text(color = "#0f1123", size = 10, face = "italic"),
axis.text.x = element_text(face = "bold", size = 12),
axis.text.y = element_text(face = "bold", size = 12)
) +
geom_label(aes(label=count),
nudge_x = 0.1,
nudge_y = 0.2,
size=5)
####
MPQualificationsDF <- medicalPractitioners %>%
group_by(`Year Range`, `Number of Qualifications`) %>%
summarise(count = n()) %>%
ggplot(aes(`Year Range`, count, group=`Number of Qualifications`)) +
geom_line(aes(color=`Number of Qualifications`), size=1) +
geom_point(aes(color=`Number of Qualifications`), size=5) +
labs(title = "Count of Qualifications of Medical Practitioners",
subtitle = "Data from 1978 to 2020",
caption = "Source:https://medicalboard.co.ke/DashBoard.php ",
x="") +
theme(
plot.title = element_text(color = "#23689b", size = 20, face = "bold",hjust = 0.5),
plot.subtitle = element_text(color = "#161d6f", size = 13, face = "bold",hjust = 0.5),
plot.caption = element_text(color = "#0f1123", size = 10, face = "italic"),
axis.text.x = element_text(face = "bold", size = 12),
axis.text.y = element_text(face = "bold", size = 12),
legend.title = element_blank(),
legend.position = "top"
) +
geom_label(aes(label=count),
nudge_x = 0.15,
nudge_y = 4,
size=4)
## Top Specialties in the last decade ####
SpecialtiesDF <- medicalPractitioners %>%
count(Specialty, sort = T) %>%
filter(n>20)
ggplot(SpecialtiesDF, aes(reorder(Specialty, n), n)) +
geom_col(aes(fill=Specialty)) +
coord_flip() +
labs(title = "Top Specialties of Medical Practitioners",
subtitle = "Data from 1978 to 2020",
caption = "Source:https://medicalboard.co.ke/DashBoard.php ",
x="", y="count") +
theme(
plot.title = element_text(color = "#23689b", size = 20, face = "bold",hjust = 0.5),
plot.subtitle = element_text(color = "#161d6f", size = 13, face = "bold",hjust = 0.5),
plot.caption = element_text(color = "#0f1123", size = 10, face = "italic"),
axis.text.x = element_text(face = "bold", size = 12),
axis.text.y = element_text(face = "bold", size = 12),
legend.title = element_blank(),
legend.position = "none"
) +
geom_label(aes(label=n),
nudge_x = 0.15,
nudge_y = 0.9,
size=4)
# Shiny Dashboard Framework ####
town <- unique(medicalPractitioners$Town)
# Define UI for application that draws a histogram
ui <- fluidPage(
dashboardPage(skin = "yellow",
dashboardHeader(
title = "KMPDC Data Dashboard",
titleWidth = 250
),
dashboardSidebar(
sidebarMenu(
sidebarSearchForm(textId = "Search", buttonId = "searchTown",
label = "Search Town")
)
),
dashboardBody(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "style.css")
),
fluidRow(
tabBox(width = 12, height = NULL, selected = "Count of Practitioners",
tabPanel("Count of Practitioners", plotlyOutput("practitioners_count")),
tabPanel("Number of Qualifications per Medical Practitioners", plotlyOutput("qualifications_count")),
tabPanel("Top Specialties of Medical Practitioners", plotlyOutput("specialityCount"))
)
)
) # End of dashboardBody()
) # End of dashboardPage()
)
server <- function(input, output) {
filtered_data <- reactive({
medicalPractitioners %>%
filter(Town %in% toupper(input$Search))
})
# Server Output - Count of Practitioners ####
output$practitioners_count <- renderPlotly({
filter(medicalPractitioners, Town==toupper(input$Search)) %>%
group_by(`Year Range`) %>%
summarise(count = n()) %>%
ggplot(aes(`Year Range`, count)) +
geom_line(aes(group = 1),color="#E9896A", size=1) +
geom_point(size=4, color="#00bfc4") +
labs(
subtitle = "Data from 1978 to 2020",
caption = "Source: medicalboard.co.ke",
x="") +
theme(
# plot.title = element_text(color = "#23689b", size = 13, face = "bold",hjust = 0.5),
plot.subtitle = element_text(color = "#161d6f", size = 9, face = "bold",hjust = 0.5),
plot.caption = element_text(color = "#0f1123", size = 10, face = "italic"),
axis.text.x = element_text(face = "bold", size = 8),
axis.text.y = element_text(face = "bold", size = 8)
) +
geom_label(aes(label=count),
nudge_x = 0.1,
nudge_y = 0.2,
size=5) +
theme_minimal()
})
# Server Output - Count of Qualifications ####
output$qualifications_count <- renderPlotly({
filter(medicalPractitioners, Town==toupper(input$Search)) %>%
group_by(`Year Range`, `Number of Qualifications`) %>%
summarise(count = n()) %>%
ggplot(aes(`Year Range`, count, group=`Number of Qualifications`)) +
geom_line(aes(color=`Number of Qualifications`), size=1) +
geom_point(aes(color=`Number of Qualifications`), size=3) +
labs(
subtitle = "Data from 1978 to 2020",
caption = "Source:https://medicalboard.co.ke/DashBoard.php ",
x="") +
theme(
# plot.title = element_text(color = "#23689b", size = 20, face = "bold",hjust = 0.5),
plot.subtitle = element_text(color = "#161d6f", size = 13, face = "bold",hjust = 0.5),
plot.caption = element_text(color = "#0f1123", size = 10, face = "italic"),
axis.text.x = element_text(face = "bold", size = 8),
axis.text.y = element_text(face = "bold", size = 8),
legend.title = element_blank(),
legend.position = "top"
) +
geom_label(aes(label=count),
nudge_x = 0.15,
nudge_y = 4,
size=2) +
theme_minimal()
})
# Server Output - Top Specialties ####
output$specialityCount <- renderPlotly({
filter(medicalPractitioners, Town==toupper(input$Search)) %>%
count(Specialty, sort = T) %>%
filter(n>20) %>%
ggplot(aes(reorder(Specialty, n), n)) +
geom_col(aes(fill=Specialty)) +
coord_flip() +
labs(
subtitle = "Data from 1978 to 2020",
caption = "Source:https://medicalboard.co.ke/DashBoard.php ",
x="", y="count") +
theme(
# plot.title = element_text(color = "#23689b", size = 20, face = "bold",hjust = 0.5),
plot.subtitle = element_text(color = "#161d6f", size = 13, face = "bold",hjust = 0.5),
plot.caption = element_text(color = "#0f1123", size = 10, face = "italic"),
axis.text.x = element_text(face = "bold", size = 8),
axis.text.y = element_text(face = "bold", size = 8),
legend.title = element_blank(),
legend.position = "none"
) +
geom_label(aes(label=n),
nudge_x = 0.15,
nudge_y = 0.9,
size=2) +
theme_minimal()
})
}
# Run the application
shinyApp(ui = ui, server = server)