0
votes

I am working on a map for my internship. I have the shapefile of the city of Villeneuve d'Ascq's subdivisions (referred to as "IRIS" in France) that I am mapping, and separate CSVs with various variables which I would like to fill the map with (i.e. population, active workforce, avg family size, etc.).

I thought that I had successfully merged the files and built up the layers of my map in R Studio using leaflet (and referring to this page in particular). The shapefile itself seems to create the appropriate borders, and when I preview the [sp dataframe?] it appears that all 31 IRIS are there with their corresponding population repeated for each point of its polygon.

The colors seemed less varied in R Studio than when I tested the shapefile in Tableau Public, but I thought it was just a less exaggerated color gradient. However, when I added the highlight labels I found that the 31 IRIS were filled with the same 4 IRIS's data : Triolo Est, Triolo Ouest, Valmy, Veterans. The only obvious pattern is that alphabetically these are the last 4 IRIS in my list.

Here's what it looks like with one IRIS associated wth the wrong IRIS and another IRIS associated with that same, incorrect IRIS...

This level of plotting / using shapefiles is something I'm just discovering for my internship, so I've tried to fortify and change the fillColor and switch ~pal but sort of haphazardly. The full code is included below, so I'm hoping there is some blatant mistake that someone else can spot that would explain why this is happening!

Thank you in advance for any feedback or help, and wishing you good health!

#-------------------Cartographie------------------------
#install
install.packages("leaflet", dependencies = TRUE)
install.packages("ggplot2")
install.packages("dplyr")
install.packages("maps")
install.packages("viridis")
install.packages("magrittr")
install.packages("rgdal")
install.packages("tigris")
install.packages("sp")
install.packages("sf")

#load at open
library(leaflet)
# ?setView
library(ggplot2)
library(dplyr)
library(maps)
library(viridis)
library(magrittr)
library(rgdal)
library(tigris)
library(sp)
library(tidyverse)
library(sf)
library(plyr)

#--------------------Map of Villeneuve d'Ascq--------------------------
va <- leaflet() %>% addTiles() %>%
  setView(lng = 3.1507164960962663, lat = 50.63599565519086, zoom = 13)  %>% addProviderTiles(providers$OpenStreetMap.France, options = providerTileOptions(opacity = 0.85))
va

#-----------Villeneuve d'Ascq Shapefile---------------------
vdascqfront <- readOGR("vascq-shapefile/VASCQiris.shp", layer = "VASCQiris")

#map.df <- fortify(vdascqfront, region ="Polygon")
#map.df <- as.data.frame(map.df)
#mapa.df <- join(map.df, vdascqfront@data, by="id")

#va %>% addPolygons(data=map.df, weight = 2)

#---------Combining Map with .csv data---------
#thanks to 
# https://github.com/ft-interactive/R-tutorials/blob/master/How%20to%20join%20a%20shapefile%20with%20a%20csv/join_shp_with_csv.md
pop <- read_excel("poptotiris.xls", col_types = c("text", "text", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric"))
pop %>% head(5)
pop <- as.data.frame(pop)

vdascqfront@data %>%  head(3)

#Rename the column in CSV to match up later
pop <- pop %>% 
  rename(NOM_IRIS=Iris)

names(pop)

as.data.frame(pop)

mergedData <- vdascqfront %>% 
  merge.data.frame(pop,by='NOM_IRIS', all.x = TRUE, all.y = TRUE)

mergedData %>% names

#make sure it's not just full of NAs even though this didn't work for me ?
mergedData %>% as.data.frame %>%  filter(!is.na("2007")) %>% nrow 

mergedData

#-----------Spectrum colors for data-------
# https://rstudio.github.io/leaflet/choropleths.html
min(mergedData$`X2007`) 
max(mergedData$`X2007`)

bins <- c(0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, Inf)
pal <- colorBin("GnBu", domain = mergedData$`X2007`)
pal

#-----------Displaying map with color fill, highlights, and legend-------

va %>% 
  addPolygons(data=vdascqfront, fill = FALSE, weight = 2)

#Color
va %>% 
  addPolygons(data=vdascqfront, fillColor = ~pal(mergedData$X2007), weight = 1, opacity = 1,
              color = "white",
              dashArray = "2",
              fillOpacity = 0.2,
              highlight = highlightOptions(
                weight = 5,
                color = "#666",
                dashArray = "",
                fillOpacity = 0.7))
#Highlight
labels <- sprintf(
  "<strong>%s</strong><br/>%g habitants",
  mergedData$NOM_IRIS, mergedData$X2007
) %>% lapply(htmltools::HTML)

vahighlight <- va %>% 
  addPolygons(data=vdascqfront, fillColor = ~pal(mergedData$`X2007`), weight = 1, opacity = 1,
              color = "white",
              dashArray = "2",
              fillOpacity = 0.2,
              highlight = highlightOptions(
                weight = 5,
                color = "#666",
                dashArray = "",
                fillOpacity = 0.7,
                bringToFront = TRUE),
              label = labels,
              labelOptions = labelOptions(
                style = list("font-weight" = "normal", padding = "3px 8px"),
                textsize = "15px",
                direction = "auto"))
#Legend
vahighlight %>% addLegend(pal = pal, values = mergedData$`X2007`, opacity = 0.7, title = "Population (en effectifs)", position = "bottomright") 

edit in case this helps:

> sessionInfo()

R version 3.6.1 (2019-07-05)

Platform: x86_64-apple-darwin15.6.0 (64-bit)

Running under: OS X El Capitan 10.11.6

Matrix products: default

BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib

LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
1

1 Answers

0
votes

Fixed the problem! With merge.data.frame I didn't have a warning message but the data obviously wasn't merging correctly. The left_join function resulted in an error message, which was why I searched for this other solution, however the error does not block any further action that the data frame joins up correctly, resulting in a perfect map!

Here's the final code :

#Map of Villeneuve d'Ascq
va <- leaflet() %>% addTiles() %>%
  setView(lng = 3.1507164960962663, lat = 50.63599565519086, zoom = 13)  %>% addProviderTiles(providers$OpenStreetMap.France, options = providerTileOptions(opacity = 0.85))
va
#Villeneuve Frontières
irisfront <- readOGR("iris-2013-01-01-2/iris-2013-01-01.shp", layer = "iris-2013-01-01")
VAirisfront <- subset(irisfront, irisfront$DEPCOM %in% c(59009
))

va %>% addPolygons(data=VAirisfront, weight = 2)

#Combining Map with .csv data
pop <- read_excel("poptotiris.xls")
pop <- as.data.frame(pop)
pop <- pop %>% 
  rename(NOM_IRIS=Iris)
names(pop)
as.data.frame(pop)

mergedData <- left_join(VAirisfront@data, pop, by = 'NOM_IRIS')
mergedData %>% names

class(mergedData)

#make sure it's not just full of NAs
mergedData %>% as.data.frame %>%  filter(!is.na("2007")) %>% nrow

And then the rest is the leaflet code for actually using the dataframe to fill the polygons etc!