cjerzak commited on
Commit
42b4218
·
verified ·
1 Parent(s): c332469

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +23 -25
app.R CHANGED
@@ -1,8 +1,6 @@
1
- # =============================
2
- # Global options & libraries
3
- # =============================
4
  library(shiny)
5
- library(shinydashboard) # or bslib / thematic, if desired
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", "Representation Gap", "Ethnicity",
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
- # Make sure "Country" and "Population" columns are present:
57
- # e.g., rename columns if your CSV differs
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) # keep only relevant columns
 
66
  })
67
 
68
- # ---- Join data and create cartogram ----
69
  cartogram_sf <- reactive({
70
- # Join your data on "name"
71
  merged_sf <- world_sf() %>%
72
- left_join(rankings_data(), by = "name")
 
73
 
74
- # cartogram_cont scales polygon area proportionally to your "Population" column
75
- # If you do NOT have a "Population" column, you could use "pop_est" from
76
- # the natural earth data or from your CSV if provided.
77
- cartogram_cont(merged_sf, weight = "Population", prepare = TRUE)
 
 
 
 
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 Population, colored by selected Index",
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"),