Show Sidebar Hide Sidebar

geom_sf in ggplot2

How to use geom_sf with Plotly.

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

## [1] '4.9.1'


In order to complete the examples below, you'll require installing additional packages (install.packages("packageName")): - sf

The examples below use the library simple features to read in the shape files before plotting the features with Plotly.

Basic sf


nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

p <- ggplotly(
  ggplot(nc) +
  geom_sf(aes(fill = AREA))


Using Native Plotly

Alternatively, you can use plot_ly, plot_geo, or plot_mapbox.


See for more information and chart attribute options! If you would like to read more on visualizing geo-spatial data with sf and ggplotly click here.