Learning How to Use Shapefiles in R - r

I am trying to learn more about "shapefiles" and how to plot them in R. Ideally, I would like to make a "leaflet map" (interactive or static) that looks something like this: https://i.cbc.ca/1.5587407.1591305080!/fileImage/httpImage/covid-19-cases-in-the-city-of-toronto-since-may-1.jpg
For this example, I found a publicly available dataset that contains information about Canadian Postal Codes - I created a new random variable that represents "number of hockey players" in each postal code:
# download folder from here : https://www.serviceobjects.com/blog/free-zip-code-and-postal-code-database-with-geocoordinates/
#extract canadian part (CanadianPostalCodes202204) - call it "canada"
canada = read.csv("CanadianPostalCodes202204.csv")
# extract Ontario
some_prov <- canada[which(canada$PROVINCE_ABBR == "ON"), ]
# create some random variable for this example
some_prov$number_of_hockey_players = as.integer(rnorm(nrow(some_prov), 87, 10))
POSTAL_CODE CITY PROVINCE_ABBR TIME_ZONE LATITUDE LONGITUDE number_of_hockey_players
11 L7C 2T6 CALEDON EAST ON 5 43.81709 -79.81907 88
17 M1K 1W6 SCARBOROUGH ON 5 43.71187 -79.26526 98
21 N5Y 4N3 LONDON ON 5 43.02114 -81.22003 72
22 N4N 1J3 HANOVER ON 5 44.14945 -81.03273 87
24 N2M 3R9 KITCHENER ON 5 43.43482 -80.48112 99
26 L1C 0E4 BOWMANVILLE ON 5 43.92937 -78.68348 90
I know how to make a general leaflet map for this data - this looks something like this:
library(leaflet)
some_prov %>%
leaflet() %>%
addTiles() %>%
addMarkers(clusterOption=markerClusterOptions())
I would now like to make a map similar to the COVID map above. Doing some research online, it seems that making such a map might require a "shapefile", or some information that tells the map where to put the desired boundaries. For example, I found the following website that might be useful (https://www.gis-blog.com/create-a-leaflet-map-using-r/ - I changed "USA" to "CANADA"):
#load leaflet package for R
library(leaflet)
library(raster)
#arbitrary data (code from above)
canada <- getData("GADM", country="canada", level=2)
canada$number_of_hockey_players <- as.integer(rnorm(n=nrow(canada), 150, 30))
#create a color palette to fill the polygons
pal <- colorQuantile("Greens", NULL, n = 5)
#create a pop up (onClick)
polygon_popup <- paste0("<strong>Name: </strong>", canada$NAME_1, "<br>",
"<strong>Indicator: </strong>", round(canada$number_of_hockey_players,2))
#create leaflet map
map = leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView(-98.35, 39.7,
zoom = 4) %>%
addPolygons(data = canada,
fillColor= ~pal(number_of_hockey_players),
fillOpacity = 0.4,
weight = 2,
color = "white",
popup = polygon_popup)
map
But I am not sure if this is the best way to do things.
I think I found some publicly available shapefile over here : https://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/bound-limit-2011-eng.cfm - but I am not sure how to correctly use these files to accomplish my goal. I am currently searching for other relevant shapefiles.
Can someone please show me how I can take this dataset that I created and make a map (interactive or static) similar to the COVID map above (with either postal code boundaries, city boundaries, etc.) ?
Thank you!

Related

Using specific variables with spatialpolygons for Leaflet mapping

I am trying to produce a map of income levels in various council areas in Sydney using leaflet. While I was able to produce circlemarkers, I have been hoping to produce the same results in polygons.
I have come across a shapefile from Councils in New South Wales, which I have managed to import to R and produce some maps in there, and then I tried merging that spatialpolygondataframe with my income dataframe using the council name, but this wasn't getting me the results that I hoped for. Below is my code, where newobj is my combined dataframe.
leaflet() %>%
addProviderTiles("CartoDB.Positron", options= providerTileOptions(opacity = 0.99)) %>%
addPolygons(data = newobj$`Crimes against the person`,
stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5
)
I basically received a few errors in there and could not produce the map.
I actually just managed to find another solution using TMAP package on R, with adding the details of my dataset to the data table of my spatial polygons.

Remove unused GEOID in geo_join

I am attempting to plot profitability on top of counties in Minnesota, Iowa, and Nebraska. Using leaflet and tigris, I have been able to plot ALL counties, whether or not I have data for it. This leaves me with a few counties with colors and the rest labeled as NA. Is there a way for me to remove all NA's from my geo_join data so that it just isn't used ala unused Wisconsin areas? I have tried using fortify, but I can't figure out how to determine what county boundaries I'm looking at when I merge the TIGER boundary lines with my County FIPS file in order to remove them.
Here is what my leaflet currently looks like:
My code to get the map is this:
library(tigris)
library(leaflet)
pal <- colorNumeric(c("yellow","dark red"),county$Construction.Cost,na.color="white")
IA_counties <- counties(state="IA", cb=TRUE, resolution ="20m")
MN_counties <- counties(state="MN",cb=TRUE,resolution="20m")
NE_counties <- counties(state="NE",cb=TRUE,resolution="20m")
IA_merged <- geo_join(IA_counties,county,"GEOID", "GEOID")
MN_merged <- geo_join(MN_counties,county,"GEOID","GEOID")
NE_merged <- geo_join(NE_counties,county,"GEOID","GEOID")
popupIA <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(IA_merged$Construction.Cost, 0), big.mark=',', format = 'f')))))
popupMN <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(MN_merged$Construction.Cost, 0), big.mark=',', format = 'f')))))
popupNE <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(NE_merged$Construction.Cost, 0), big.mark=',', format = 'f')))))
leaflet() %>%
addProviderTiles("MapQuestOpen.OSM") %>%
addLegend(pal = pal,
values = IA_merged$Construction.Cost,
position = "bottomright",
title = "County Projects",
labFormat=labelFormat(prefix="$")) %>%
addCircles(lng=yup2$lon, lat=yup2$lat,weight=.75,fillOpacity=0.01,color="red",
radius = 96560) %>%
addCircles(lng=yup2$lon, lat=yup2$lat,weight=.75,fillOpacity=0.01,color="blue",
radius = 193121) %>%
addPolygons(data = IA_counties,
fillColor = ~pal(IA_merged$Construction.Cost),
layerId=1,
fillOpacity = .25,
weight = 0.05,
popup = popupIA)%>%
addPolygons(data=MN_counties,
fillColor=~pal(MN_merged$Construction.Cost),
fillOpacity=0.25,
weight=0.05,
popup = popupMN) %>%
addPolygons(data=NE_counties,
fillColor=~pal(NE_merged$Construction.Cost),
fillOpacity=0.25,
weight=0.05,
popup = popupNE)
I apologize for not including reproducible data, but if needed, please ask. I'm hoping that this is more of a simple na.color= formula solution. The map looks "okay" as of now, but I'd like it if it's possible to not have to make the fillOpacity so light so the NA counties don't stand out.
Thanks for any and all help and please, let me know if you have any questions!
I'm the creator of the tigris package. Thanks so much for using it! In the development version of tigris on GitHub (https://github.com/walkerke/tigris), I've added an option to geo_join to accommodate inner joins, which would remove the unmatched data entirely from the resultant spatial data frame (if this is what you are looking for). You can also supply a common merge column name as a named argument to the new by parameter if you want. For example:
IA_merged <- geo_join(IA_counties, county, by = "GEOID", how = "inner")
should work. I'm still testing but I'll probably submit this update to CRAN in January.
So, embarrassingly, the answer to this question was as simple as I had hoped. I tweaked the following na.color code and it worked exactly as I wanted.
pal <- colorNumeric(c("yellow","dark red"),county$Construction.Cost,na.color="transparent")

Plotting regions and adding color in leaflet R

I need to plot a map of Denmark divided into regions (there are 5: Region Nordjylland, Midtjylland, Sydjylland, Sjælland and Hovedstaden) and then color the regions such that the different regions stand out clearly. I have to use leaflet, since it has other features, that I will use later. I found a map on naturalearthdata.com, that I think I can use, but I can't figure out how to color (or even indicate) the regions. The code I tried is below
library(rgdal)
library(leaflet)
download.file(file.path('http://www.naturalearthdata.com/http/',
'www.naturalearthdata.com/download/50m/cultural',
'ne_50m_admin_1_states_provinces_lakes.zip'),
f <- tempfile())
unzip(f, exdir=tempdir())
world <- readOGR(tempdir(), 'ne_50m_admin_1_states_provinces_lakes', encoding='UTF-8')
DK <- subset(world, name=="Denmark")
leaflet() %>% addTiles() %>% addTopoJSON(DK, weight = 1, color = "#444444", fill = TRUE)
How does one use the naturalearthdata.com data to plot regions/states/provinces of different countries? I have seen a very nice example at
http://www.56n.dk/kort/dk2050kort_age.html
but there is no sample code available.
I have also found a very nice example here: https://rpubs.com/walkerke/leaflet_choropleth - but I need a map of Denmark.
UPDATE: I have found a shapefile at http://www.kortforsyningen.dk which does the trick. So now my question is how do I combine my own data with a shapefile and plot it in leaflet? If I just put
DK <- readOGR(".../shape", layer="REGION")
leaflet(data=DK)
I get a blank screen...

R Leaflet doesn't add all markers

I'm trying to follow the example in the link below to create a map with all the markers
Tutorial: How to put dots on a Leaflet map with R
The source file is below
https://www.dropbox.com/s/az1yolknqwoxhb4/test_file.csv?dl=0
And the code that I tried
library(dplyr)
library(leaflet)
test_map <- read.csv("test_file.csv", header = TRUE, stringsAsFactors = FALSE)
m <- leaflet(test_map) %>% addTiles('http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png',
attribution='Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap')
m %>% setView()
m %>% addCircles(~long, ~lat,
popup=test_map$index,
weight = 3,
color="#ffa500", stroke = TRUE, fillOpacity = 0.8)
I was able to create the map
However, the map only shows a fraction of points, since the data I have has locations all over Canada. When I tried to sub-select a city say like Toronto then some of the missing points shows up.
I'm not sure if i'm doing anything wrong or if this is a bug.I wonder if there's anyway for me to fix this problem or is there an alternative way to achieve a similar map?
Thank you :)
There are NA values in test_map variable.
add
test_map <- na.omit(test_map)
after reading csv.
By this method i have more markers than your image.

NASA tiles with leaflet in R

I would like to ask some help with regard to the leaflet package. When you draw an interactive map, you can do something like this.
library(leaflet)
library(magrittr)
m <- leaflet() %>%
setView(lng = -71.0589, lat = 42.3601, zoom = 8) %>%
addTiles()
m
If you want to add a third-party tile, you can do that too. The following link offers options for third-party tiles (http://leaflet-extras.github.io/leaflet-providers/preview/index.html) The following image is created with OpenWeatherMap.Precipitation.
### They work
m %>% addProviderTiles("MtbMap")
m %>% addProviderTiles("HikeBike.HikeBike")
m %>% addProviderTiles("OpenWeatherMap.Precipitation")
Some of the tile options in the link include tiles by NASA. I wanted to use one of them. So I tried the following codes. None of them unfortunately worked.
### The default map appears, then a black layer appears on top of the default layer.
m %>% addProviderTiles("NASAGIBS.ModisTerraTRUEColorCR")
m %>% addProviderTiles("NASAGIBS.ModisTerraBands367CR")
The only option which is working is the following.
m %>% addProviderTiles("NASAGIBS.ViirsEarthAtNight2012")
My next attempt was to use custom URL template with addTiles(). The URL is from the link above. But, this was not successful either; no error message appeared, but no change in tile.
m %>%addTiles(urlTemplate = "http://map1.vis.earthdata.nasa.gov/wmts-webmerc/MODIS_Terra_CorrectedReflectance_Bands367/default/{time}/{tilematrixset}{maxZoom}/{z}/{y}/{x}.{format}",
tileOptions(minZoom = 1, maxZoom = 8))
My final attempt was the following. This showed the default map, but an additional tile did not appear either.
leaflet() %>%
addTiles() %>%
setView(lng = -71.0589, lat = 42.3601, zoom = 8) %>%
addTiles(urlTemplate = "http://map1.vis.earthdata.nasa.gov/wmts-webmerc/MODIS_Terra_CorrectedReflectance_Bands367/default/{time}/{tilematrixset}{maxZoom}/{z}/{y}/{x}.{format}",
tileOptions(minZoom = 1, maxZoom = 8))
My question is whether this is a potential bug specifically with NASA tiles. Alternatively, what revision do I need in these scripts? Thank you for your help in advance.
UPDATE
I found a website which uses same NASA tiles. I specified NASAGIBS.ModisTerraTRUEColorCR and got the following image. The image is showing how a mail traveled from the US to Sweden. As you see, there is no image for both US and Europe. I think this could be the reason why I saw a black tile. I would like to know if anybody knows some details of NASA tiles. I chose the area which I can see the NASA image. But, I had no luck.
### I expected to see Japan area this time.
foo <- leaflet() %>%
setView(lng = 137.37, lat = 35.93, zoom = 5) %>%
addTiles()
foo %>% addProviderTiles("NASAGIBS.ModisTerraTRUEColorCR")
UPDATE 2
Today, I gave one more shot. At this moment, I managed to get the following image. I zoomed out a bit when I captured it. In UPDATE, I provided a map which you cannot see the US and Europe. In the new image, you see West coast of the States is in black. Given all observations, it seems to me that one may not get NASA images of a location all the time. Depending on when you ask NASA tiles, you may/may not have an image you want.
m <- leaflet() %>%
setView(lng = -71.0589, lat = 42.3601, zoom = 8) %>%
addTiles()
m %>% addProviderTiles("NASAGIBS.ModisTerraBands367CR")
Your final conclusion is correct: depending on what location you request imagery for and on the time of the request, the satellite may or may not yet have acquired the image. So you may get an actual image or just an empty one. (This is also stated in the GIBS API documentation.)
However, you can specify what day to request the image for via the 'time' option for addProviderTiles(). By specifying a date in the near past, you can get non-empty images for all locations if that is what you prefer.
This is the syntax:
> library(leaflet)
> library(magrittr)
> m <- leaflet() %>%
setView(lng = 4.5, lat = 51, zoom = 1) %>%
addTiles() %>%
addProviderTiles("NASAGIBS.ModisTerraTrueColorCR",
options = providerTileOptions(time = "2015-08-31", opacity = 0.5))
> m
At the time of writing (2015-08-31) I get this result:
It's cloudy in England, who would have guessed?
Most data happens to be there already, but there is no imagery for Alaska yet. If on the other hand, I specify yesterday's date
options = providerTileOptions(time = "2015-08-30", opacity = 0.5)
we get the full image:
Finally, the reason why
m %>% addProviderTiles("NASAGIBS.ModisTerraTRUEColorCR")
didn't work was probably because of a typo. It should be
m %>% addProviderTiles("NASAGIBS.ModisTerraTrueColorCR")

Resources