Load Data

musees <- read.csv("/Users/lilyewing/Desktop/Fall23/Comp112/Chloropleth_midterm/frequentation-des-musees-de-france.csv", sep = ";" )

head(musees)
##   ref_musee                                   nom_du_musee annee
## 1   8109902 Muséum d'histoire naturelle Philadelphe Thomas  2009
## 2   8111501                 Musée Départemental du Textile  2009
## 3   8200801                        Musée du Vieil Auvillar  2009
## 4   8211202        Musée des arts et traditions populaires  2009
## 5   8215501                           Musée de Préhistoire  2009
## 6   5903601              Musée d'Histoire et d'Archéologie  2009
##              regions                   ville date_appellation payant gratuit
## 1      MIDI-PYRENEES                 GAILLAC       01/02/2003    789    3683
## 2      MIDI-PYRENEES    LA-BASTIDE-ROUAIROUX       01/02/2003   2608    3139
## 3      MIDI-PYRENEES                AUVILLAR       01/02/2003   2892    1778
## 4      MIDI-PYRENEES                 MOISSAC       01/02/2003   4082       0
## 5      MIDI-PYRENEES SAINT-ANTONIN-NOBLE-VAL       01/02/2003    674       0
## 6 NORD-PAS-DE-CALAIS       AVESNES-SUR-HELPE       01/02/2003     NA      NA
##   total note id_museofile          nomdep observations
## 1  4472                              TARN             
## 2  5747                              TARN             
## 3  4670                   TARN-ET-GARONNE             
## 4  4082                   TARN-ET-GARONNE             
## 5   674                   TARN-ET-GARONNE             
## 6    NA    F                         NORD
france <- sf::read_sf("/Users/lilyewing/Desktop/Fall23/Comp112/Chloropleth_midterm/gadm41_FRA_shp/gadm41_FRA_1.shp")

Data Wrangling

mainland <- musees %>%
  filter(annee == "2015") %>%
  mutate(regions = stringi::stri_trans_general(str = regions, id = "Latin-ASCII")) %>%
  mutate(regions = tolower(regions)) %>%
  group_by(regions) %>%
  summarize(n = n()) %>%
  filter(regions != "guyane") %>%
  filter(regions != "guadeloupe") %>%
  filter(regions != "martinique") %>%
  filter(regions != "mayotte") %>%
  filter(regions != "la reunion" ) %>%
  filter(regions != "st pierre et miquelon") 
moderne_regions <- pivot_wider(data = mainland, names_from = regions, values_from = n) %>%
  mutate("grand est" = sum(alsace, `champagne-ardennes`, `lorraine`)) %>%
  mutate("nouvelle-aquitaine" = sum(aquitaine, limousin, `poitou-charentes`)) %>%
  mutate("auvergne-rhone-alpes" = sum(`rhone-alpes`, auvergne)) %>%
  mutate("occitanie" = sum(`languedoc-roussillon`, `midi-pyrenees`)) %>%
  mutate("bourgogne-franche-comte" = sum(bourgogne, `franche-comte`)) %>%
  mutate("normandie" = sum(`basse-normandie`, `haute-normandie`)) %>%
  mutate("hauts-de-france" = sum(`picardie`, `nord-pas-de-calais`)) %>%
  mutate("centre-val de loire" = centre)

longer_regions <- pivot_longer(data = moderne_regions, cols = everything(), names_to = "region", values_to = "n" )

filtered <- longer_regions %>%
  filter(region != "alsace") %>%
  filter(region != "champagne-ardennes") %>%
  filter(region != "lorraine") %>%
  filter(region != "aquitaine") %>%
  filter(region != "poitou-charentes") %>%
  filter(region != "rhone-alpes") %>%
  filter(region != "auvergne") %>%
  filter(region != "limousin") %>%
  filter(region != "languedoc-roussillon") %>%
  filter(region != "rhone-alpes") %>%
  filter(region != "languedoc-roussillon") %>%
  filter(region != "midi-pyrenees") %>%
  filter(region != "bourgogne") %>%
  filter(region != "franche-comte") %>%
  filter(region != "basse-normandie") %>%
  filter(region != "haute-normandie") %>%
  filter(region != "nord-pas-de-calais") %>%
  filter(region != "picardie")


france_letters <- france %>%
  mutate(region = stringi::stri_trans_general(str = NAME_1, id = "Latin-ASCII")) %>%
  mutate(region = tolower(region))

musee_data <- left_join(france_letters, filtered, by = "region")
musee_data <- musee_data %>% rename_at('NAME_1', ~'Name')
museum_map <- musee_data %>%
  ggplot(linewidth = 2, aes(label = Name)) +
  geom_sf(data = musee_data %>% filter(Name != "Corse"), color = "white", aes(fill = n)) +
 geom_sf(data = musee_data %>% filter(Name == "Corse"), color = "black", aes(fill = n)) +
   geom_sf_label(fill = "white",  
                fun.geometry = sf::st_point_on_surface) +
  labs(title = "Museums in France per Region, 2015", 
       fill = " ",
       caption = "created by Lily Ewing, Oct 25, 2023 \n Data from République Française Fréquentation des Musées de France \n https://www.data.gouv.fr/fr/datasets/frequentation-des-musees-de-france-1/", x = NULL, y = NULL) +
  scale_fill_gradient(low = "white", high = "deepskyblue4") +
  theme(axis.text = element_blank(), 
        axis.ticks=element_blank(), 
       axis.title = element_blank(),
       panel.grid = element_blank(),
       panel.border = element_blank(),
    panel.background = element_blank())

plotly_build(museum_map)
## Warning in st_point_on_surface.sfc(data$geometry): st_point_on_surface may not
## give correct results for longitude/latitude data
## Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomLabel() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

Acknowledgements

https://stackoverflow.com/questions/39148759/remove-accents-from-a-dataframe-column-in-r user Oscar Montoya on removing accents

https://stackoverflow.com/questions/63334436/pivot-longer-on-all-columns kmacierzanka on how to select all columns in pivot

https://www.gouvernement.fr/actualite/de-22-a-13-regions

https://stackoverflow.com/questions/36325154/how-to-choose-variable-to-display-in-tooltip-when-using-ggplotly user Uwe for adding tooltip with plotly