Update app.R
Browse files
app.R
CHANGED
|
@@ -8,7 +8,7 @@ library(rnaturalearth)
|
|
| 8 |
library(rnaturalearthdata)
|
| 9 |
library(countrycode)
|
| 10 |
library(ggplot2)
|
| 11 |
-
library(ggiraph)
|
| 12 |
|
| 13 |
# =============================
|
| 14 |
# UI
|
|
@@ -55,7 +55,7 @@ ui <- dashboardPage(
|
|
| 55 |
.sidebar-menu, .sidebar-menu li a, .sidebar-menu .fa {
|
| 56 |
font-family: 'OCR A Extended', monospace !important;
|
| 57 |
}
|
| 58 |
-
|
| 59 |
/* Header gradient background */
|
| 60 |
.main-header .navbar {
|
| 61 |
background: linear-gradient(to right, #3b6978, #204051) !important;
|
|
@@ -82,19 +82,19 @@ ui <- dashboardPage(
|
|
| 82 |
border-left-color: #78cdd7 !important;
|
| 83 |
color: #ffffff !important;
|
| 84 |
}
|
| 85 |
-
|
| 86 |
/* Sidebar menu item icons */
|
| 87 |
.sidebar-menu .fa {
|
| 88 |
color: #78cdd7 !important;
|
| 89 |
}
|
| 90 |
-
|
| 91 |
/* Sidebar menu item text */
|
| 92 |
.sidebar-menu > li > a {
|
| 93 |
color: #b8c7ce !important;
|
| 94 |
font-size: 15px;
|
| 95 |
font-weight: 500;
|
| 96 |
}
|
| 97 |
-
|
| 98 |
/* Customize the boxes */
|
| 99 |
.box {
|
| 100 |
border-top: none !important;
|
|
@@ -127,7 +127,7 @@ ui <- dashboardPage(
|
|
| 127 |
width = 12,
|
| 128 |
title = strong("Global Leadership Project (GLP)"),
|
| 129 |
solidHeader = TRUE,
|
| 130 |
-
#
|
| 131 |
div(
|
| 132 |
style = "height: 80vh; padding: 10px;",
|
| 133 |
girafeOutput("cartogramPlot", width = "100%", height = "100%")
|
|
@@ -144,45 +144,37 @@ ui <- dashboardPage(
|
|
| 144 |
# =============================
|
| 145 |
server <- function(input, output, session) {
|
| 146 |
|
| 147 |
-
#
|
| 148 |
-
custom_iso_matches <- c("Kosovo" = "XKX",
|
| 149 |
-
"Somaliland" = "SOM") # or any valid code you prefer
|
| 150 |
-
|
| 151 |
-
# 2. Read CSV data and create ISO3 codes with custom matches
|
| 152 |
rankings_data <- reactive({
|
| 153 |
-
read_csv("CountryRepresentationRankings.csv"
|
| 154 |
-
mutate(iso_a3 = countrycode(
|
| 155 |
-
sourcevar = Country,
|
| 156 |
-
origin = "country.name",
|
| 157 |
-
destination = "iso3c",
|
| 158 |
-
custom_match = custom_iso_matches
|
| 159 |
-
))
|
| 160 |
})
|
| 161 |
|
| 162 |
-
#
|
| 163 |
world_sf <- reactive({
|
| 164 |
ne_countries(scale = "medium", returnclass = "sf") %>%
|
| 165 |
dplyr::select(name, iso_a3, pop_est, geometry) %>%
|
| 166 |
st_transform(crs = "ESRI:54009") # Mollweide projection
|
| 167 |
})
|
| 168 |
|
| 169 |
-
#
|
| 170 |
cartogram_sf <- reactive({
|
| 171 |
merged_sf <- world_sf() %>%
|
| 172 |
left_join(rankings_data(), by = "iso_a3")
|
| 173 |
-
# Filter out
|
| 174 |
merged_sf <- merged_sf[!is.na(merged_sf$Overall),]
|
| 175 |
-
merged_sf
|
| 176 |
})
|
| 177 |
|
| 178 |
-
#
|
| 179 |
output$cartogramPlot <- renderGirafe({
|
| 180 |
req(input$indexChoice)
|
| 181 |
|
| 182 |
plot_data <- cartogram_sf()
|
| 183 |
index_col <- input$indexChoice
|
| 184 |
|
| 185 |
-
# Build a tooltip string
|
|
|
|
| 186 |
plot_data$tooltip_text <- paste0(
|
| 187 |
"<b>Country:</b> ", plot_data$Country, "<br/>",
|
| 188 |
"<b>Overall:</b> ", ifelse(!is.na(plot_data$Overall), plot_data$Overall, "N/A"), "<br/>",
|
|
@@ -193,21 +185,20 @@ server <- function(input, output, session) {
|
|
| 193 |
"<b>Language:</b> ", ifelse(!is.na(plot_data$Language), plot_data$Language, "N/A")
|
| 194 |
)
|
| 195 |
|
| 196 |
-
#
|
| 197 |
p <- ggplot(plot_data) +
|
| 198 |
geom_sf_interactive(
|
| 199 |
-
|
| 200 |
-
fill =
|
| 201 |
-
tooltip = tooltip_text,
|
| 202 |
-
data_id = iso_a3
|
| 203 |
),
|
| 204 |
color = "grey20",
|
| 205 |
size = 0.1
|
| 206 |
) +
|
| 207 |
scale_fill_viridis_c(option = "D", na.value = "white") +
|
| 208 |
coord_sf(expand = FALSE) +
|
| 209 |
-
|
| 210 |
-
theme_void(base_size = 14, base_family = "sans") +
|
| 211 |
labs(
|
| 212 |
fill = paste(index_col, "Index"),
|
| 213 |
title = "Country Representation Rankings",
|
|
@@ -223,17 +214,14 @@ server <- function(input, output, session) {
|
|
| 223 |
legend.key.width = unit(2, "cm")
|
| 224 |
)
|
| 225 |
|
| 226 |
-
# Wrap the ggplot in a girafe object
|
| 227 |
girafe(
|
| 228 |
ggobj = p,
|
| 229 |
width_svg = 10,
|
| 230 |
height_svg = 6,
|
| 231 |
options = list(
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
font-family: 'OCR A Extended', monospace;
|
| 235 |
-
color: black;"
|
| 236 |
-
)
|
| 237 |
)
|
| 238 |
)
|
| 239 |
})
|
|
|
|
| 8 |
library(rnaturalearthdata)
|
| 9 |
library(countrycode)
|
| 10 |
library(ggplot2)
|
| 11 |
+
library(ggiraph) # <-- for interactive tooltips
|
| 12 |
|
| 13 |
# =============================
|
| 14 |
# UI
|
|
|
|
| 55 |
.sidebar-menu, .sidebar-menu li a, .sidebar-menu .fa {
|
| 56 |
font-family: 'OCR A Extended', monospace !important;
|
| 57 |
}
|
| 58 |
+
|
| 59 |
/* Header gradient background */
|
| 60 |
.main-header .navbar {
|
| 61 |
background: linear-gradient(to right, #3b6978, #204051) !important;
|
|
|
|
| 82 |
border-left-color: #78cdd7 !important;
|
| 83 |
color: #ffffff !important;
|
| 84 |
}
|
| 85 |
+
|
| 86 |
/* Sidebar menu item icons */
|
| 87 |
.sidebar-menu .fa {
|
| 88 |
color: #78cdd7 !important;
|
| 89 |
}
|
| 90 |
+
|
| 91 |
/* Sidebar menu item text */
|
| 92 |
.sidebar-menu > li > a {
|
| 93 |
color: #b8c7ce !important;
|
| 94 |
font-size: 15px;
|
| 95 |
font-weight: 500;
|
| 96 |
}
|
| 97 |
+
|
| 98 |
/* Customize the boxes */
|
| 99 |
.box {
|
| 100 |
border-top: none !important;
|
|
|
|
| 127 |
width = 12,
|
| 128 |
title = strong("Global Leadership Project (GLP)"),
|
| 129 |
solidHeader = TRUE,
|
| 130 |
+
# Instead of plotOutput, we use girafeOutput for interactive tooltips
|
| 131 |
div(
|
| 132 |
style = "height: 80vh; padding: 10px;",
|
| 133 |
girafeOutput("cartogramPlot", width = "100%", height = "100%")
|
|
|
|
| 144 |
# =============================
|
| 145 |
server <- function(input, output, session) {
|
| 146 |
|
| 147 |
+
# ---- Read CSV data and create ISO3 codes ----
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
rankings_data <- reactive({
|
| 149 |
+
read_csv("CountryRepresentationRankings.csv") %>%
|
| 150 |
+
mutate(iso_a3 = countrycode(Country, origin = "country.name", destination = "iso3c"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
})
|
| 152 |
|
| 153 |
+
# ---- Read/prepare world map shapefile ----
|
| 154 |
world_sf <- reactive({
|
| 155 |
ne_countries(scale = "medium", returnclass = "sf") %>%
|
| 156 |
dplyr::select(name, iso_a3, pop_est, geometry) %>%
|
| 157 |
st_transform(crs = "ESRI:54009") # Mollweide projection
|
| 158 |
})
|
| 159 |
|
| 160 |
+
# ---- Create cartogram (currently a regular map) ----
|
| 161 |
cartogram_sf <- reactive({
|
| 162 |
merged_sf <- world_sf() %>%
|
| 163 |
left_join(rankings_data(), by = "iso_a3")
|
| 164 |
+
# Filter out NA values in 'Overall' (or whichever main column) to avoid missing countries
|
| 165 |
merged_sf <- merged_sf[!is.na(merged_sf$Overall),]
|
| 166 |
+
return(merged_sf)
|
| 167 |
})
|
| 168 |
|
| 169 |
+
# ---- Plot output (using ggiraph for tooltips) ----
|
| 170 |
output$cartogramPlot <- renderGirafe({
|
| 171 |
req(input$indexChoice)
|
| 172 |
|
| 173 |
plot_data <- cartogram_sf()
|
| 174 |
index_col <- input$indexChoice
|
| 175 |
|
| 176 |
+
# Build a tooltip string showing some key columns for each country.
|
| 177 |
+
# Modify this as desired to include more (or fewer) details.
|
| 178 |
plot_data$tooltip_text <- paste0(
|
| 179 |
"<b>Country:</b> ", plot_data$Country, "<br/>",
|
| 180 |
"<b>Overall:</b> ", ifelse(!is.na(plot_data$Overall), plot_data$Overall, "N/A"), "<br/>",
|
|
|
|
| 185 |
"<b>Language:</b> ", ifelse(!is.na(plot_data$Language), plot_data$Language, "N/A")
|
| 186 |
)
|
| 187 |
|
| 188 |
+
# Create a ggplot with geom_sf_interactive so we can assign tooltips
|
| 189 |
p <- ggplot(plot_data) +
|
| 190 |
geom_sf_interactive(
|
| 191 |
+
aes_string(
|
| 192 |
+
fill = index_col,
|
| 193 |
+
tooltip = "tooltip_text",
|
| 194 |
+
data_id = "iso_a3" # required for hover/click tracking
|
| 195 |
),
|
| 196 |
color = "grey20",
|
| 197 |
size = 0.1
|
| 198 |
) +
|
| 199 |
scale_fill_viridis_c(option = "D", na.value = "white") +
|
| 200 |
coord_sf(expand = FALSE) +
|
| 201 |
+
theme_void(base_size = 14, base_family = "Courier New") +
|
|
|
|
| 202 |
labs(
|
| 203 |
fill = paste(index_col, "Index"),
|
| 204 |
title = "Country Representation Rankings",
|
|
|
|
| 214 |
legend.key.width = unit(2, "cm")
|
| 215 |
)
|
| 216 |
|
| 217 |
+
# Wrap the ggplot in a girafe object
|
| 218 |
girafe(
|
| 219 |
ggobj = p,
|
| 220 |
width_svg = 10,
|
| 221 |
height_svg = 6,
|
| 222 |
options = list(
|
| 223 |
+
# You can style the tooltip background here
|
| 224 |
+
opts_tooltip(css = "background-color: white; font-family: 'OCR A Extended', monospace;")
|
|
|
|
|
|
|
|
|
|
| 225 |
)
|
| 226 |
)
|
| 227 |
})
|