-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjunk.R
230 lines (191 loc) · 7.26 KB
/
junk.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
library(shiny)
library(ggplot2)
library(ggbump)
library(dplyr)
library(stringr)
library(plotly)
library(readxl)
library(httr)
library(tidyr)
source("charts.R")
# Define the server logic
shinyServer(function(input, output, session) {
# Default metric
observe({
if (is.null(input$metric)) {
updateSelectInput(session, "metric", selected = "Overall Score")
}
})
submetric_choices <- reactive({
switch(input$metric,
"Overall Score" = NULL,
"Bridges" = NULL,
"Congestion Hours" = NULL,
"Pavement Roughness" = c("Urban Interstate Pavement Roughness",
"Rural Interstate Pavement Roughness",
"Urban Opa Pavement Roughness",
"Rural Opa Pavement Roughness"),
"Disbursement" = c("Admin Disbursement",
"Capital Disbursement",
"Maintenance Disbursement",
"Other Disbursement"),
"Fatalities" = c("Rural Fatalities",
"Urban Fatalities",
"Other Fatalities"),
NULL) # Default case
})
observe({
updateSelectInput(session, "submetric", choices = submetric_choices())
})
output$submetric_ui <- renderUI({
if (!is.null(input$metric) && is.null(submetric_choices())) {
return(NULL)
}
selectInput("submetric", "Submetric:", choices = submetric_choices())
})
output$interactivePlot <- renderPlotly({
req(input$metric)
# Check if the selected metric does not have submetrics
if (input$metric %in% c("Overall Score", "Bridges", "Congestion Hours")) {
submetric <- NULL
} else {
req(input$submetric)
submetric <- input$submetric
}
# Generate the interactive plot
create_ranking_plot(ranking_df, input$metric, submetric)
})
})
#### BUMP chart
library(readxl)
library(httr)
library(tidyr)
library(ggplot2)
library(stringr)
library(dplyr)
library(ggbump)
library(plotly)
library(viridisLite)
library(maps)
library(sf)
library(wesanderson)
####Preparing data####
url <- "https://raw.githubusercontent.com/thuy2020/Annual-Highway-Report/report_2022/output/AHR_data_2022.xlsx"
GET(url, write_disk(tf <- tempfile(fileext = ".xlsx")))
sheet1 <- read_excel(tf, sheet = 1)
sheet3 <- read_excel(tf, sheet = 3)
sheet2 <- read_excel(tf, sheet = 2)
ranking_df <- sheet2 %>%
select(state, contains("rank")) %>%
pivot_longer(cols = -state, names_to = "category", values_to = "ranking") %>%
mutate(category = str_replace_all(category, "_", " "),
category = str_to_title(category),
category = str_remove_all(category, "(Rank)|(Poor)"),
category = str_remove_all(category, "(Per 100m Vmt)|(Percent)|(Perlm)"),
category = str_replace_all(category, "State Avg Congestion Hours", "Congestion Hours"),
category = str_squish(category))
####Map function####
states_map <- st_as_sf(map("state", plot = FALSE, fill = TRUE))
df_for_map <- ranking_df %>%
pivot_wider(names_from = category, values_from = ranking) %>%
filter(state != "United States") %>%
mutate(state = str_to_lower(state)) %>%
left_join(states_map, by = c("state" = "ID")) %>%
st_as_sf() %>%
#using a projected coordinate system (not longitude/latitude)
#(Coordinate Reference System WGS84 (crs = 4326).
st_transform(crs = 4326)
wes_palette <- wes_palette("Darjeeling1", n = 5, type = "continuous")
# Function to generate maps for each specified ranking column
create_maps <- function(df_for_map, metric) {
p_map <- ggplot(data = df_for_map) +
geom_sf(aes(geometry = geom, fill = !!rlang::sym(metric)), color = "white") +
geom_sf_text(aes(geometry = geom, label = !!rlang::sym(metric)),
check_overlap = TRUE, size = 3, color = "white") +
#scale_fill_viridis_c(option = "D", direction = -1) +
scale_fill_gradientn(colors = wes_palette) +
labs(title = paste("Rankings by", metric)) +
theme_minimal() +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
legend.position = "none")
return(p_map)
}
create_maps(df_for_map, "Overall Score")
####Bump chart function####
create_ranking_plot <- function(ranking_df, metric, submetric) {
d <- ranking_df %>%
filter(str_detect(category, metric))%>%
mutate(top_state = ifelse(ranking <= 10 & category == submetric, "Top 10", "Others")) %>%
mutate(category_wrapped = str_wrap(category, width = 10))
# Set colors based on the metric
line_color <- switch(tolower(metric),
"disbursement" = "#FF5733",
"fatalities" = "#C70039",
"pavement roughness" = "purple")
# Get the rightmost category
rightmost_category <- max(d$category_wrapped)
# Create a ggbump chart highlighting top 10 states
p <- d %>%
ggplot(aes(x = category_wrapped, y = ranking, group = state,
color = top_state, size = top_state,
text = paste("State: ", state, "<br>Category: ", category, "<br>Ranking: ", ranking))) +
geom_bump() +
geom_point(aes(color = top_state, size = top_state)) +
geom_text(data = d %>% filter(category == submetric &
top_state == "Top 10"),
aes(x = rightmost_category,label = state),
hjust = 0, vjust = 0.5, size = 1.5, nudge_x = 0.3, color = line_color) +
scale_color_manual(values = c("Top 10" = line_color, "Others" = "gray")) +
scale_size_manual(values = c("Top 10" = 0.5, "Others" = 0.1)) +
theme_minimal() +
labs(title = paste("Rankings by", str_to_title(metric)),
x = "",
y = "") +
theme(
legend.position = "none",
axis.text.x = element_text(vjust = 0.5, hjust = 1)
)
# Convert the ggplot2 plot to an interactive plot using plotly
interactive_plot <- ggplotly(p, tooltip = "text")
# Customize the tooltip appearance
interactive_plot <- interactive_plot %>%
layout(hoverlabel = list(
bgcolor = "white",
font = list(size = 10)
))
# Display the interactive plot
return(interactive_plot)
}
#Testing
metric <- "Pavement Roughness"
submetric <- "Urban Opa Pavement Roughness"
create_ranking_plot(ranking_df, metric, submetric)
####No submetric ranking####
point_color = c("Overall Score" = "#008E75",
"Bridges" = "#900C3F",
"Congestion Hours" = "#CA3311")
# Define the function to generate plots for each metric
no_submetric_ranking_plots <- function(ranking_df, metrics) {
for (metric in metrics) {
p <- ranking_df %>%
filter(category == metric) %>%
ggplot(aes(x = ranking, y = reorder(state, ranking))) +
geom_col(fill = "gray", width = 0.1) +
geom_point(size = 3, color = point_color[metric]) +
theme_minimal() +
labs(title = paste("Rankings by", metric),
x = "Ranking",
y = "")+
theme(axis.text.y = element_text(size = 6))
return(p)
}
}
# List of metrics to loop through
metrics <- c("Bridges")
#, "Bridges", "Congestion Hours"
# Generate the plots
no_submetric_ranking_plots(ranking_df, metrics)