#LOAD packages
library(sf)
library(tidyverse)
library(rnaturalearth)
library(rnaturalearthdata)STAT 118: Notes L
Plotting Points on Maps
#LOAD shapefiles
#ne countries retrieves sf shapefiles for countries around the world
world <- ne_countries(returnclass = "sf",scale="medium")
class(world)[1] "sf" "data.frame"
More sf maps with points
Let’s make a global map which shows country borders.
#basic map
ggplot(data=world)+
geom_sf(fill = "ivory", color = "black")+
theme_bw()We are going to visualize global fishing landings in 2012 from a UN FAO report that ranked the top 100 fishing ports by metric landings.
The dataframe ports contains 8 variables:
port- Port namecountry- Country of the portlandings_metrictons- Metric tonnage of landings in 2012latitude- Port latitudelongitude- Port longitudedemersal_perc- Percentage of demersal speciespelagic_perc- Percentage of pelagic speciesshellfish_perc- Percentage of shellfish species
ports<-read.csv("../data/landings.csv")
#let's add our sites to our map
world%>%
ggplot() +
geom_sf(fill = "ivory", color = "black") +
theme_bw()+
geom_point(data=ports,aes(x=longitude,y=latitude))+
theme()Just like in non-map ggplot, we can alter the aesthetics of the port points by another variable in our dataframe. Here, we will change the size of our points based on the weight of fisheries landings.
#size by number oflandings
world%>%
ggplot() +
geom_sf(fill = "gray", color = "black") +
theme_bw()+
geom_point(data=ports,aes(x=longitude,y=latitude,size=landings_metrictons),color="navy",alpha=0.5)+
labs(size="Landings (Metric Tons)",title="Global Port Landings, 2012")We can further restrict the geographic scope of this map by first filtering our sf world shapefile to just show Japan’s borders, and then to filter the ports dataframe to just show Japanese ports.
world%>%
filter(admin=="Japan")%>%
ggplot() +
geom_sf(fill = "gray", color = "black") +
theme_bw()+
geom_point(data=ports%>%filter(country=="Japan"),aes(x=longitude,y=latitude,size=landings_metrictons),color="navy",alpha=0.5)+
labs(size="Landings (Metric Tons)",title="Global Port Landings, 2012")Leaflet
Leaflet is one of the most popular open-source JavaScript libraries for interactive maps.
library(leaflet)You create a Leaflet map with these basic steps:
- Create a map widget by calling leaflet().
- Add layers (i.e., features) to the map by using layer functions (e.g.
addTiles,addMarkers,addPolygons) to modify the map widget. - Repeat step 2 as desired.
Using OpenStreet Maps
A simple map of Tohoku University:
leaflet() %>%
addTiles() %>%
# Add default OpenStreetMap map tiles
addMarkers(lng=140.830833, lat=38.255528, popup="Tohoku University Faculty of Agriculture")A map of global landings:
leaflet(data=ports) %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=~longitude, lat=~latitude, popup=~port)or using Circle Markers:
leaflet(data=ports) %>%
addTiles() %>%# Add default OpenStreetMap map tiles
addCircleMarkers(lng=~longitude, lat=~latitude, popup=~port, radius = ~landings_metrictons/100000, stroke =FALSE, fillOpacity =0.5)