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.

library(plotly)
packageVersion('plotly')
## [1] '4.7.1.9000'

Introduction

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

library(plotly)
library(sf)

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

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

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="ggplot2-sf-1")
chart_link

Using Native Plotly

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

Reference

See https://plot.ly/r/reference/ 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.

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.