File size: 11,790 Bytes
42b4218
0dbfc34
42b4218
f02e31c
 
 
638f9f7
f02e31c
 
 
6daf1d7
8aa102f
f02e31c
638f9f7
f02e31c
 
638f9f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f02e31c
 
 
0dbfc34
638f9f7
 
 
 
 
 
 
 
 
f02e31c
 
638f9f7
f02e31c
638f9f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f02e31c
 
 
 
 
 
638f9f7
 
 
 
 
f02e31c
 
8aa102f
 
 
0dbfc34
 
f02e31c
638f9f7
f02e31c
0dbfc34
2acecda
42b4218
f02e31c
 
42b4218
8aa102f
 
f02e31c
 
 
638f9f7
6daf1d7
f02e31c
 
6daf1d7
f02e31c
 
42b4218
638f9f7
42b4218
638f9f7
8aa102f
2acecda
f02e31c
 
 
8aa102f
f02e31c
 
8aa102f
f02e31c
 
6daf1d7
 
638f9f7
 
8aa102f
f02e31c
 
d2be2d3
42b4218
8aa102f
f02e31c
638f9f7
 
 
6daf1d7
638f9f7
 
f02e31c
8aa102f
0dbfc34
 
f02e31c
 
 
638f9f7
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
options(error = NULL)
library(shiny)
library(shinydashboard)
library(dplyr)
library(readr)
library(sf)
library(cartogram)   # optional if you decide to do cartograms later
library(ggplot2)
library(rnaturalearth)
library(rnaturalearthdata)
library(countrycode)

# =============================
#            UI
# =============================
ui <- dashboardPage(
  
  # Use black skin for the dashboard
  skin = "black",
  
  dashboardHeader(
    title = span(
      style = "font-weight: 600; font-size: 18px;",
      "Country Representation"
    )
  ),
  
  dashboardSidebar(
    sidebarMenu(
      menuItem("Map Type", tabName = "cartogramTab", icon = icon("globe"))
    ),
    div(
      style = "margin: 15px;",
      selectInput(
        inputId = "indexChoice",
        label = "Select Representation Index:",
        choices = c("Overall", "RepresentationGap", "Ethnicity",
                    "Gender", "Religion", "Language"),
        selected = "Overall"
      )
    )
  ),
  
  dashboardBody(
    # Bring in the OCR A Extended font from Google Fonts
    tags$head(
      tags$link(
        href = "https://fonts.googleapis.com/css2?family=OCR+A+Extended&display=swap",
        rel = "stylesheet"
      ),
      tags$style(HTML("
        /* Force OCR A Extended font across the entire UI and all HTML elements */
        html, body, h1, h2, h3, h4, h5, h6, p, div, span, label, input, button, select,
        .box, .content-wrapper, .main-sidebar, .main-header .navbar, .main-header .logo,
        .sidebar-menu, .sidebar-menu li a, .sidebar-menu .fa {
          font-family: 'OCR A Extended', monospace !important;
        }
        
        /* Header gradient background */
        .main-header .navbar {
          background: linear-gradient(to right, #3b6978, #204051) !important;
        }

        /* Logo area (left corner of the header) */
        .main-header .logo {
          background: #1b2a2f !important;
          color: #ffffff !important;
          border-bottom: none;
          font-size: 18px;
          font-weight: 600;
        }

        /* Sidebar background */
        .main-sidebar {
          background-color: #1b2a2f !important;
        }

        /* Active or hovered tab in the sidebar */
        .sidebar-menu > li.active > a,
        .sidebar-menu > li:hover > a {
          background-color: #344e5c !important;
          border-left-color: #78cdd7 !important;
          color: #ffffff !important;
        }
        
        /* Sidebar menu item icons */
        .sidebar-menu .fa {
          color: #78cdd7 !important;
        }
        
        /* Sidebar menu item text */
        .sidebar-menu > li > a {
          color: #b8c7ce !important;
          font-size: 15px;
          font-weight: 500;
        }
        
        /* Customize the boxes */
        .box {
          border-top: none !important;
          box-shadow: 0 4px 8px rgba(0,0,0,0.1);
          border-radius: 6px;
        }
        .box.box-solid > .box-header {
          background-color: #204051;
          color: #fff;
          border-radius: 6px 6px 0 0;
        }

        /* Plot box spacing */
        .box .box-body {
          padding: 0 !important;
        }

        /* Footer text styling (plot captions, etc.) */
        .small, small {
          font-size: 75%;
        }
      "))
    ),
    
    tabItems(
      tabItem(
        tabName = "cartogramTab",
        fluidRow(
          box(
            width = 12,
            title = strong("Global Leadership Project (GLP)"),
            solidHeader = TRUE,
            div(style = "height: 80vh; padding: 10px;",
                plotOutput("cartogramPlot", height = "100%")
            )
          )
        )
      )
    )
  )
)

# =============================
#          SERVER
# =============================
server <- function(input, output, session) {
  
  # ---- Read CSV data and create ISO3 codes ----
  rankings_data <- reactive({
    read_csv("CountryRepresentationRankings.csv") %>%
      mutate(iso_a3 = countrycode(Country, origin = "country.name", destination = "iso3c"))
  })
  
  # ---- Read/prepare world map shapefile ----
  world_sf <- reactive({
    ne_countries(scale = "medium", returnclass = "sf") %>%
      dplyr::select(name, iso_a3, pop_est, geometry) %>%
      st_transform(crs = "ESRI:54009")  # Mollweide projection
  })
  
  # ---- Create cartogram (currently a regular map) ----
  cartogram_sf <- reactive({
    merged_sf <- world_sf() %>%
      left_join(rankings_data(), by = "iso_a3")
    # Filter out NA values in 'Overall' to avoid missing countries
    merged_sf <- merged_sf[!is.na(merged_sf$Overall),]
    return(merged_sf) 
  })
  
  # ---- Plot output ----
  output$cartogramPlot <- renderPlot({
    req(input$indexChoice)
    
    index_col <- input$indexChoice
    plot_data <- cartogram_sf()
    
    ggplot(plot_data) +
      geom_sf(aes_string(fill = index_col), color = "grey20", size = 0.1) +
      scale_fill_viridis_c(option = "D", na.value = "white") +
      coord_sf(expand = FALSE) +
      # Force OCR A Extended font in the plot
      #theme_void(base_size = 14, base_family = "OCR A Extended") +
      theme_void(base_size = 14, base_family = "Courier New") +
      labs(
        fill = paste(index_col, "Index"),
        title = "Country Representation Rankings",
        subtitle = "Map Colored by Selected Representation Index",
        caption = "Source: Global Leadership Project (GLP) & Natural Earth"
      ) +
      theme(
        plot.title      = element_text(face = "bold", hjust = 0.5, size = 20),
        plot.subtitle   = element_text(hjust = 0.5, size = 14),
        plot.caption    = element_text(hjust = 1, size = 10),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.key.width = unit(2, "cm")
      )
  })
}

# =============================
#   Launch the Shiny App
# =============================
shinyApp(ui = ui, server = server)
options(error = NULL)
library(shiny)
library(shinydashboard)
library(dplyr)
library(readr)
library(sf)
library(cartogram)   # optional if you decide to do cartograms later
library(ggplot2)
library(rnaturalearth)
library(rnaturalearthdata)
library(countrycode)

# =============================
#            UI
# =============================
ui <- dashboardPage(
  
  # Use black skin for the dashboard
  skin = "black",
  
  dashboardHeader(
    title = span(
      style = "font-weight: 600; font-size: 18px;",
      "Country Representation"
    )
  ),
  
  dashboardSidebar(
    sidebarMenu(
      menuItem("Cartogram", tabName = "cartogramTab", icon = icon("globe"))
    ),
    div(
      style = "margin: 15px;",
      selectInput(
        inputId = "indexChoice",
        label = "Select Representation Index:",
        choices = c("Overall", "RepresentationGap", "Ethnicity",
                    "Gender", "Religion", "Language"),
        selected = "Overall"
      )
    )
  ),
  
  dashboardBody(
    # Bring in the OCR A Extended font from Google Fonts
    tags$head(
      tags$link(
        href = "https://fonts.googleapis.com/css2?family=OCR+A+Extended&display=swap",
        rel = "stylesheet"
      ),
      tags$style(HTML("
        /* Force OCR A Extended font across the entire UI and all HTML elements */
        html, body, h1, h2, h3, h4, h5, h6, p, div, span, label, input, button, select,
        .box, .content-wrapper, .main-sidebar, .main-header .navbar, .main-header .logo,
        .sidebar-menu, .sidebar-menu li a, .sidebar-menu .fa {
          font-family: 'OCR A Extended', monospace !important;
        }
        
        /* Header gradient background */
        .main-header .navbar {
          background: linear-gradient(to right, #3b6978, #204051) !important;
        }

        /* Logo area (left corner of the header) */
        .main-header .logo {
          background: #1b2a2f !important;
          color: #ffffff !important;
          border-bottom: none;
          font-size: 18px;
          font-weight: 600;
        }

        /* Sidebar background */
        .main-sidebar {
          background-color: #1b2a2f !important;
        }

        /* Active or hovered tab in the sidebar */
        .sidebar-menu > li.active > a,
        .sidebar-menu > li:hover > a {
          background-color: #344e5c !important;
          border-left-color: #78cdd7 !important;
          color: #ffffff !important;
        }
        
        /* Sidebar menu item icons */
        .sidebar-menu .fa {
          color: #78cdd7 !important;
        }
        
        /* Sidebar menu item text */
        .sidebar-menu > li > a {
          color: #b8c7ce !important;
          font-size: 15px;
          font-weight: 500;
        }
        
        /* Customize the boxes */
        .box {
          border-top: none !important;
          box-shadow: 0 4px 8px rgba(0,0,0,0.1);
          border-radius: 6px;
        }
        .box.box-solid > .box-header {
          background-color: #204051;
          color: #fff;
          border-radius: 6px 6px 0 0;
        }

        /* Plot box spacing */
        .box .box-body {
          padding: 0 !important;
        }

        /* Footer text styling (plot captions, etc.) */
        .small, small {
          font-size: 75%;
        }
      "))
    ),
    
    tabItems(
      tabItem(
        tabName = "cartogramTab",
        fluidRow(
          box(
            width = 12,
            title = strong("Cartogram / Map"),
            solidHeader = TRUE,
            div(style = "height: 80vh; padding: 10px;",
                plotOutput("cartogramPlot", height = "100%")
            )
          )
        )
      )
    )
  )
)

# =============================
#          SERVER
# =============================
server <- function(input, output, session) {
  
  # ---- Read CSV data and create ISO3 codes ----
  rankings_data <- reactive({
    read_csv("CountryRepresentationRankings.csv") %>%
      mutate(iso_a3 = countrycode(Country, origin = "country.name", destination = "iso3c"))
  })
  
  # ---- Read/prepare world map shapefile ----
  world_sf <- reactive({
    ne_countries(scale = "medium", returnclass = "sf") %>%
      dplyr::select(name, iso_a3, pop_est, geometry) %>%
      st_transform(crs = "ESRI:54009")  # Mollweide projection
  })
  
  # ---- Create cartogram (currently a regular map) ----
  cartogram_sf <- reactive({
    merged_sf <- world_sf() %>%
      left_join(rankings_data(), by = "iso_a3")
    # Filter out NA values in 'Overall' to avoid missing countries
    merged_sf <- merged_sf[!is.na(merged_sf$Overall),]
    return(merged_sf) 
  })
  
  # ---- Plot output ----
  output$cartogramPlot <- renderPlot({
    req(input$indexChoice)
    
    index_col <- input$indexChoice
    plot_data <- cartogram_sf()
    
    ggplot(plot_data) +
      geom_sf(aes_string(fill = index_col), color = "grey20", size = 0.1) +
      scale_fill_viridis_c(option = "D", na.value = "white") +
      coord_sf(expand = FALSE) +
      # Force OCR A Extended font in the plot
      theme_void(base_size = 14, base_family = "OCR A Extended") +
      labs(
        fill = paste(index_col, "Index"),
        title = "Country Representation Rankings",
        subtitle = "Map Colored by Selected Representation Index",
        caption = "Source: Global Leadership Project (GLP) & Natural Earth"
      ) +
      theme(
        plot.title      = element_text(face = "bold", hjust = 0.5, size = 20),
        plot.subtitle   = element_text(hjust = 0.5, size = 14),
        plot.caption    = element_text(hjust = 1, size = 10),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.key.width = unit(2, "cm")
      )
  })
}

# =============================
#   Launch the Shiny App
# =============================
shinyApp(ui = ui, server = server)