Plotting GeoJSON polygons on a map with R

December 15, 2016
By

(This article was first published on Upstate Data Analysis, and kindly contributed to R-bloggers)

In a previous post we plotted some points, retrieved from a public dataset in GeoJSON format, on top of a Google Map of the area surrounding Greenville, SC. In this post we plot some public data in GeoJSON format as well, but instead of particular points, we plot polygons. Polygons describe an area rather than a single point. As before, to set up we do the following:

library(rgdal)
if (!require(geojsonio)) {
    install.packages("geojsonio")
    library(geojsonio)
}
library(sp)
library(maps)
library(ggmap)
library(maptools)

Getting the data

The data we are going to analyze consists of the city parks in Greenville, SC. Though this data is located in an ArcGIS system, there is a GeoJSON version at OpenUpstate.

data_url <- "https://data.openupstate.org/maps/city-parks/parks.php"
data_file <- "parks.geojson"
# for some reason, I can't read from the url directly, though the tutorial
# says I can
download.file(data_url, data_file)
Error in download.file(data_url, data_file): cannot open URL 'https://data.openupstate.org/maps/city-parks/parks.php'
data_park <- geojson_read(data_file, what = "sp")
Error in rgdal::ogrListLayers(input): Cannot open data source

Analyzing the data

First, we plot the data as before:

plot(data_park)
Error in plot(data_park): object 'data_park' not found

While this was easy to do, it doesn’t give very much context. However, it does give the boundaries of the different parks. As before, we use the ggmap and ggplot2 package to give us some context. First, we download from Google the right map.

mapImage <- ggmap(get_googlemap(c(lon = -82.394012, lat = 34.852619), scale = 1, 
    zoom = 11), extent = "normal")

I got the latitude and longitude by looking up on Google, and then hand-tuned the scale and zoom.

A note of warning: if you do this with a recent version of ggmap and ggplot2, you may need to download the GitHub versions. See this Stackoverflow thread for details.

Now, we prepare our spatial object for plotting. This is a more difficult process than before, and requires the use of the fortify command from ggplot2 package to make sure everything makes it to the right format:

data_park_df <- fortify(data_park)
Error in fortify(data_park): object 'data_park' not found

Now we can make the plot:

print(mapImage + geom_polygon(aes(long, lat, group = group), data = data_park_df, 
    colour = "green"))
Error in fortify(data): object 'data_park_df' not found

Note the use of the group= option in the geom_polygon function above. This tells geom_polygon that there are many polygons rather than just one. Without that option, you get a big mess:

print(mapImage + geom_polygon(aes(long, lat), data = data_park_df, colour = "green"))
Error in fortify(data): object 'data_park_df' not found

Mashup of parking convenient to Swamp Rabbit Trail and city parks

Now, say you want to combine the city parks data with the parking places convenient to Swamp Rabbit Trail that was the subject of the last post. That is very easy using the ggplot2 package. We get the data and manipulate it as last time:

Error in download.file(data_parking_url, data_parking_file): cannot open URL 'https://data.openupstate.org/maps/swamp-rabbit-trail/parking/geojson.php'
Error in rgdal::ogrListLayers(input): Cannot open data source
Error in as.data.frame(data_parking): object 'data_parking' not found
Error in names(data_parking_df)[4:5] <- c("long", "lat"): object 'data_parking_df' not found

Next, we use the layering feature of ggplot2 to draw the map:

Error in fortify(data): object 'data_parking_df' not found

Conclusions

We continue to explore public geographical data by examining data representing areas in addition to points. In addition, we layer data from two sources.

To leave a comment for the author, please follow the link and comment on their blog: Upstate Data Analysis.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)