cjerzak commited on
Commit
6daf1d7
·
verified ·
1 Parent(s): dc3df2e

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +16 -23
app.R CHANGED
@@ -8,7 +8,7 @@ library(cartogram)
8
  library(ggplot2)
9
  library(rnaturalearth)
10
  library(rnaturalearthdata)
11
- library(countrycode)
12
 
13
  # =============================
14
  # UI
@@ -19,7 +19,6 @@ ui <- dashboardPage(
19
  sidebarMenu(
20
  menuItem("Cartogram", tabName = "cartogramTab", icon = icon("globe"))
21
  ),
22
- # User input: which representation index to display
23
  selectInput(
24
  inputId = "indexChoice",
25
  label = "Select Representation Index:",
@@ -35,7 +34,7 @@ ui <- dashboardPage(
35
  fluidRow(
36
  box(
37
  width = 12,
38
- plotOutput("cartogramPlot", height = "600px")
39
  )
40
  )
41
  )
@@ -51,32 +50,22 @@ server <- function(input, output, session) {
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 ----
@@ -88,8 +77,9 @@ server <- function(input, output, session) {
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") +
92
- theme_minimal(base_size = 14) +
 
93
  labs(
94
  fill = paste(index_col, "Index"),
95
  title = "Country Representation Rankings",
@@ -97,9 +87,12 @@ server <- function(input, output, session) {
97
  caption = "Source: Global Leadership Project (GLP) & Natural Earth"
98
  ) +
99
  theme(
100
- plot.title = element_text(face = "bold"),
101
- axis.text = element_blank(),
102
- axis.ticks = element_blank()
 
 
 
103
  )
104
  })
105
  }
@@ -107,4 +100,4 @@ server <- function(input, output, session) {
107
  # =============================
108
  # Launch the Shiny App
109
  # =============================
110
- shinyApp(ui = ui, server = server)
 
8
  library(ggplot2)
9
  library(rnaturalearth)
10
  library(rnaturalearthdata)
11
+ library(countrycode)
12
 
13
  # =============================
14
  # UI
 
19
  sidebarMenu(
20
  menuItem("Cartogram", tabName = "cartogramTab", icon = icon("globe"))
21
  ),
 
22
  selectInput(
23
  inputId = "indexChoice",
24
  label = "Select Representation Index:",
 
34
  fluidRow(
35
  box(
36
  width = 12,
37
+ div(style = "height: 80vh;", plotOutput("cartogramPlot", height = "100%"))
38
  )
39
  )
40
  )
 
50
  # ---- Read CSV data and create ISO3 codes ----
51
  rankings_data <- reactive({
52
  read_csv("CountryRepresentationRankings.csv") %>%
 
53
  mutate(iso_a3 = countrycode(Country, origin = "country.name", destination = "iso3c"))
54
  })
55
 
56
  # ---- Read/prepare world map shapefile ----
57
  world_sf <- reactive({
58
  ne_countries(scale = "medium", returnclass = "sf") %>%
59
+ select(name, iso_a3, pop_est, geometry) %>%
60
+ st_transform(crs = "ESRI:54009") # Mollweide projection
61
  })
62
 
63
+ # ---- Create cartogram (currently a regular map) ----
64
  cartogram_sf <- reactive({
 
65
  merged_sf <- world_sf() %>%
66
  left_join(rankings_data(), by = "iso_a3")
67
  merged_sf <- merged_sf[!is.na(merged_sf$Overall),]
68
+ return(merged_sf) # Regular map; cartogram functions are commented out
 
 
 
 
 
 
 
 
69
  })
70
 
71
  # ---- Plot output ----
 
77
 
78
  ggplot(plot_data) +
79
  geom_sf(aes_string(fill = index_col), color = "grey20", size = 0.1) +
80
+ scale_fill_viridis_c(option = "D", na.value = "white") +
81
+ coord_sf(expand = FALSE) +
82
+ theme_void(base_size = 14) +
83
  labs(
84
  fill = paste(index_col, "Index"),
85
  title = "Country Representation Rankings",
 
87
  caption = "Source: Global Leadership Project (GLP) & Natural Earth"
88
  ) +
89
  theme(
90
+ plot.title = element_text(face = "bold", hjust = 0.5),
91
+ plot.subtitle = element_text(hjust = 0.5),
92
+ plot.caption = element_text(hjust = 1),
93
+ plot.margin = unit(c(0, 0, 0, 0), "cm"),
94
+ legend.position = "bottom",
95
+ legend.direction = "horizontal"
96
  )
97
  })
98
  }
 
100
  # =============================
101
  # Launch the Shiny App
102
  # =============================
103
+ shinyApp(ui = ui, server = server)