|
|
|
|
|
|
|
library(shiny) |
|
library(shinydashboard) |
|
library(dplyr) |
|
library(readr) |
|
library(sf) |
|
library(cartogram) |
|
library(ggplot2) |
|
library(rnaturalearth) |
|
library(rnaturalearthdata) |
|
|
|
|
|
|
|
|
|
ui <- dashboardPage( |
|
dashboardHeader(title = "Country Representation Rankings"), |
|
dashboardSidebar( |
|
sidebarMenu( |
|
menuItem("Cartogram", tabName = "cartogramTab", icon = icon("globe")) |
|
), |
|
|
|
selectInput( |
|
inputId = "indexChoice", |
|
label = "Select Representation Index:", |
|
choices = c("Overall", "Representation Gap", "Ethnicity", |
|
"Gender", "Religion", "Language"), |
|
selected = "Overall" |
|
) |
|
), |
|
dashboardBody( |
|
tabItems( |
|
tabItem( |
|
tabName = "cartogramTab", |
|
fluidRow( |
|
box( |
|
width = 12, |
|
plotOutput("cartogramPlot", height = "600px") |
|
) |
|
) |
|
) |
|
) |
|
) |
|
) |
|
|
|
|
|
|
|
|
|
server <- function(input, output, session) { |
|
|
|
|
|
|
|
rankings_data <- reactive({ |
|
read_csv("CountryRepresentationRankings.csv") %>% |
|
|
|
|
|
rename(name = Country) |
|
}) |
|
|
|
|
|
world_sf <- reactive({ |
|
|
|
ne_countries(scale = "medium", returnclass = "sf") %>% |
|
select(name, iso_a3, geometry) |
|
}) |
|
|
|
|
|
cartogram_sf <- reactive({ |
|
|
|
merged_sf <- world_sf() %>% |
|
left_join(rankings_data(), by = "name") |
|
|
|
|
|
|
|
|
|
cartogram_cont(merged_sf, weight = "Population", prepare = TRUE) |
|
}) |
|
|
|
|
|
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") + |
|
theme_minimal(base_size = 14) + |
|
labs( |
|
fill = paste(index_col, "Index"), |
|
title = "Country Representation Rankings", |
|
subtitle = "Cartogram sized by Population, colored by selected Index", |
|
caption = "Source: Global Leadership Project (GLP)" |
|
) + |
|
theme( |
|
plot.title = element_text(face = "bold"), |
|
axis.text = element_blank(), |
|
axis.ticks = element_blank() |
|
) |
|
}) |
|
} |
|
|
|
|
|
|
|
|
|
shinyApp(ui = ui, server = server) |
|
|