Skip to content

Instantly share code, notes, and snippets.

@lmullen
Last active January 26, 2022 22:07
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lmullen/8375785 to your computer and use it in GitHub Desktop.
Save lmullen/8375785 to your computer and use it in GitHub Desktop.
How I use shapefiles in R with ggplot2 and RGDAL
library(rgdal) # R wrapper around GDAL/OGR
library(ggplot2) # for general plotting
library(ggmaps) # for fortifying shapefiles
# First read in the shapefile, using the path to the shapefile and the shapefile name minus the
# extension as arguments
shapefile <- readOGR("path/to/shapefile/", "name_of_shapefile")
# Next the shapefile has to be converted to a dataframe for use in ggplot2
shapefile_df <- fortify(shapefile)
# Now the shapefile can be plotted as either a geom_path or a geom_polygon.
# Paths handle clipping better. Polygons can be filled.
# You need the aesthetics long, lat, and group.
map <- ggplot() +
geom_path(data = shapefile_df,
aes(x = long, y = lat, group = group),
color = 'gray', fill = 'white', size = .2)
print(map)
# Using the ggplot2 function coord_map will make things look better and it will also let you change
# the projection. But sometimes with large shapefiles it makes everything blow up.
map_projected <- map +
coord_map()
print(map_projected)
@SiteSelector
Copy link

Thanks for the fortify tip!

@tommarmstrong
Copy link

Thanks for this. Was really helpful. I think line 3 should import ggmap not ggmaps though :)

@ricardoochoa
Copy link

ricardoochoa commented Dec 15, 2016

Thanks for this. I might be wrong, but if I recall well, the library's name is ggmap, and not ggmaps. Cheers!

@aaabuabat
Copy link

Thank u so much, it is very helpful!

@xiaohk
Copy link

xiaohk commented Feb 3, 2018

Thanks fo much!

Just a quick question, it says Ignoring unknown parameters: fill for geom_path(). How can I fix it please?

@josephlewis
Copy link

If you didn't work it out, Xiaohk, the fill parameter only work for geom_polygon.

@arijeet777
Copy link

fortify did the trick!

@jadianes
Copy link

jadianes commented May 8, 2018

Thanks!

@mikenyawo
Copy link

Thanks. Fortify did the trick for me

@z458r456
Copy link

I think "ggmaps" is now "ggmap"

@MokeEire
Copy link

It seems as if broom::tidy() is becoming the preferred method of converting shapefiles to dataframes.

The function description has changed to:

Rather than using this function, I now recommend using the broom package, which implements a much wider range of methods. fortify may be deprecated in the future.

@bcgovjz
Copy link

bcgovjz commented Mar 19, 2019

is it possible to read from github directly? ie i have r script and shapefile/geodatabase all on github, how can i connect them?

@mlobo4370
Copy link

Hi. when using the converted shapefile the map appears distorted, how can I solve it?
They know how to color each province according to the prevalence of something, with plot me it was much easier. Thank you

@gauravdewan
Copy link

Thanks a lot! fortify is the best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment