Update app.R
Browse files
app.R
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
-
|
2 |
-
# Global options & libraries
|
3 |
-
# =============================
|
4 |
library(shiny)
|
5 |
-
library(shinydashboard)
|
6 |
library(dplyr)
|
7 |
library(readr)
|
8 |
library(sf)
|
@@ -10,6 +8,7 @@ library(cartogram)
|
|
10 |
library(ggplot2)
|
11 |
library(rnaturalearth)
|
12 |
library(rnaturalearthdata)
|
|
|
13 |
|
14 |
# =============================
|
15 |
# UI
|
@@ -24,7 +23,7 @@ ui <- dashboardPage(
|
|
24 |
selectInput(
|
25 |
inputId = "indexChoice",
|
26 |
label = "Select Representation Index:",
|
27 |
-
choices = c("Overall", "
|
28 |
"Gender", "Religion", "Language"),
|
29 |
selected = "Overall"
|
30 |
)
|
@@ -49,45 +48,44 @@ ui <- dashboardPage(
|
|
49 |
# =============================
|
50 |
server <- function(input, output, session) {
|
51 |
|
52 |
-
# ---- Read CSV data ----
|
53 |
-
# Modify the path to your CSV as needed
|
54 |
rankings_data <- reactive({
|
55 |
read_csv("CountryRepresentationRankings.csv") %>%
|
56 |
-
#
|
57 |
-
|
58 |
-
rename(name = Country) # rename to match natural earth "name" field
|
59 |
})
|
60 |
|
61 |
# ---- Read/prepare world map shapefile ----
|
62 |
world_sf <- reactive({
|
63 |
-
# Download a simple polygons set
|
64 |
ne_countries(scale = "medium", returnclass = "sf") %>%
|
65 |
-
select(name, iso_a3, geometry) #
|
|
|
66 |
})
|
67 |
|
68 |
-
# ----
|
69 |
cartogram_sf <- reactive({
|
70 |
-
#
|
71 |
merged_sf <- world_sf() %>%
|
72 |
-
left_join(rankings_data(), by = "
|
|
|
73 |
|
74 |
-
#
|
75 |
-
# If you
|
76 |
-
#
|
77 |
-
|
|
|
|
|
|
|
|
|
78 |
})
|
79 |
|
80 |
# ---- Plot output ----
|
81 |
output$cartogramPlot <- renderPlot({
|
82 |
req(input$indexChoice)
|
83 |
|
84 |
-
# The user’s chosen index to display
|
85 |
index_col <- input$indexChoice
|
86 |
-
|
87 |
-
# Prepare the cartogram for plotting
|
88 |
plot_data <- cartogram_sf()
|
89 |
|
90 |
-
# Basic ggplot: color fill by the chosen index
|
91 |
ggplot(plot_data) +
|
92 |
geom_sf(aes_string(fill = index_col), color = "grey20", size = 0.1) +
|
93 |
scale_fill_viridis_c(option = "D", na.value = "white") +
|
@@ -95,8 +93,8 @@ server <- function(input, output, session) {
|
|
95 |
labs(
|
96 |
fill = paste(index_col, "Index"),
|
97 |
title = "Country Representation Rankings",
|
98 |
-
subtitle = "Cartogram sized by
|
99 |
-
caption = "Source: Global Leadership Project (GLP)"
|
100 |
) +
|
101 |
theme(
|
102 |
plot.title = element_text(face = "bold"),
|
|
|
1 |
+
options(error = NULL)
|
|
|
|
|
2 |
library(shiny)
|
3 |
+
library(shinydashboard)
|
4 |
library(dplyr)
|
5 |
library(readr)
|
6 |
library(sf)
|
|
|
8 |
library(ggplot2)
|
9 |
library(rnaturalearth)
|
10 |
library(rnaturalearthdata)
|
11 |
+
library(countrycode)
|
12 |
|
13 |
# =============================
|
14 |
# UI
|
|
|
23 |
selectInput(
|
24 |
inputId = "indexChoice",
|
25 |
label = "Select Representation Index:",
|
26 |
+
choices = c("Overall", "RepresentationGap", "Ethnicity",
|
27 |
"Gender", "Religion", "Language"),
|
28 |
selected = "Overall"
|
29 |
)
|
|
|
48 |
# =============================
|
49 |
server <- function(input, output, session) {
|
50 |
|
51 |
+
# ---- Read CSV data and create ISO3 codes ----
|
|
|
52 |
rankings_data <- reactive({
|
53 |
read_csv("CountryRepresentationRankings.csv") %>%
|
54 |
+
# Use countrycode to convert the country names to ISO3 codes
|
55 |
+
mutate(iso_a3 = countrycode(Country, origin = "country.name", destination = "iso3c"))
|
|
|
56 |
})
|
57 |
|
58 |
# ---- Read/prepare world map shapefile ----
|
59 |
world_sf <- reactive({
|
|
|
60 |
ne_countries(scale = "medium", returnclass = "sf") %>%
|
61 |
+
select(name, iso_a3, pop_est, geometry) %>% # includes iso_a3 for merging
|
62 |
+
st_transform(crs = "ESRI:54009") # projected coordinate system
|
63 |
})
|
64 |
|
65 |
+
# ---- Create cartogram ----
|
66 |
cartogram_sf <- reactive({
|
67 |
+
# Merge your CSV data (for coloring) with Natural Earth polygons via ISO3
|
68 |
merged_sf <- world_sf() %>%
|
69 |
+
left_join(rankings_data(), by = "iso_a3")
|
70 |
+
merged_sf <- merged_sf[!is.na(merged_sf$Overall),]
|
71 |
|
72 |
+
# You can choose which variable to use for the cartogram distortion.
|
73 |
+
# If you want to size by pop_est, use weight = "pop_est"
|
74 |
+
#cartogram_cont(
|
75 |
+
#merged_sf,
|
76 |
+
#weight = "pop_est",
|
77 |
+
#prepare = TRUE)
|
78 |
+
#return( cartogram_dorling( merged_sf, weight = "pop_est" ))
|
79 |
+
return( merged_sf )
|
80 |
})
|
81 |
|
82 |
# ---- Plot output ----
|
83 |
output$cartogramPlot <- renderPlot({
|
84 |
req(input$indexChoice)
|
85 |
|
|
|
86 |
index_col <- input$indexChoice
|
|
|
|
|
87 |
plot_data <- cartogram_sf()
|
88 |
|
|
|
89 |
ggplot(plot_data) +
|
90 |
geom_sf(aes_string(fill = index_col), color = "grey20", size = 0.1) +
|
91 |
scale_fill_viridis_c(option = "D", na.value = "white") +
|
|
|
93 |
labs(
|
94 |
fill = paste(index_col, "Index"),
|
95 |
title = "Country Representation Rankings",
|
96 |
+
subtitle = "Cartogram sized by pop_est, colored by selected Index",
|
97 |
+
caption = "Source: Global Leadership Project (GLP) & Natural Earth"
|
98 |
) +
|
99 |
theme(
|
100 |
plot.title = element_text(face = "bold"),
|