# Load packageslibrary(tidyverse)library(countrycode)library(maps)library(plotly)library(reactable)# Import survey datad <-read_csv("maze_169169456_block_9ddce458-867d-4f3b-85a4-93d015455926_sessions.csv")# Standardise country namesd_countries <- d |>mutate(country =countrycode(value, origin ="country.name", destination ="country.name", origin_regex =TRUE)) # Fix country text not matchedd_clean <- d_countries |>mutate(country =case_when(toupper(value) =="NL"~"Netherlands", value =="England"~"United Kingdom", value =="the U.K."~"United Kingdom", value =="CH\n"~"Switzerland", value =="中国"~"China", value =="PRC"~"China", value =="China, Australia"~"China", value =="México"~"Mexico", value =="Brasil"~"Brazil",TRUE~ country))# Add continentsd_clean <- d_clean |>mutate(continent =countrycode(country, origin ="country.name", destination ="continent"))# Get counts per countrycountry_counts <- d_clean |>group_by(country) |>mutate(n =n()) |>select(continent, country, n) |>distinct() |>mutate(country =replace_na(country, "NA"), continent =replace_na(continent, "NA")) # some countries can't match (asad, 1) so show as NA# Plot mapworld_map <-map_data("world")world_map <-subset(world_map, region !="Antarctica")# Standarise the region to the country names world_map <- world_map |>mutate(country =countrycode(region, origin ="country.name", destination ="country.name", origin_regex =TRUE))# Merge the datasetsworld_map <- world_map %>%left_join(country_counts) |>mutate(n =replace_na(n, 0))# Plotgg <-ggplot() +geom_polygon(data = world_map, aes(x = long, y = lat, group = group, fill = n, text =paste(country, n, sep ="\n")), colour ="grey", linewidth =0.25) +scale_fill_gradient(low ="white", high ="red") +theme_minimal() +theme(legend.position ="bottom") +labs(fill ="n") ggplotly(gg, tooltip ="text") |>layout(legend =list(orientation ="h", x =0.25, y =-0.02)) |>partial_bundle() # reduce html file size