Lines and Shapes

Leaflet makes it easy to take spatial lines and shapes from R and add them to maps.

Polygons and Polylines

Line and polygon data can come from a variety of sources:

  • Objects of SpatialPolygons, SpatialPolygonsDataFrame, Polygon, and Polygons class (from the sp package)
  • Objects of SpatialLines, SpatialLinesDataFrame, Lines, and Line class (from the sp package)
  • Objects of map class (from the maps package’s map() function); use map(fill = TRUE) for polygons, FALSE for polylines
  • Numeric matrix with two columns

When a numeric matrix is used, the first column is longitude and the second is latitude. Polygons are separated by rows of (NA, NA). It is not possible to represent polygons with holes using this method – use SpatialPolygons instead.


# From
states <- readOGR("shp/cb_2013_us_state_20m.shp",
  layer = "cb_2013_us_state_20m", verbose = FALSE)

neStates <- subset(states, states$STUSPS %in% c(

leaflet(neStates) %>%
    stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5,
    color = ~colorQuantile("YlOrRd", states$AWATER)(AWATER)


Circles are added using addCircles(). Circles are similar to circle markers; the only difference is that circles have their radii specified in meters, while circle markers are specified in pixels. As a result, circles are scaled with the map as the user zooms in and out, while circle markers remain a constant size on the screen regardless of zoom level.

When plotting circles, only the circle centers (and radii) are required, so the set of valid data sources is different than for polygons and the same as for markers. See the introduction to Markers for specifics.

cities <- read.csv(textConnection("
New York City,40.7127,-74.0059,8406000

leaflet(cities) %>% addTiles() %>%
  addCircles(lng = ~Long, lat = ~Lat, weight = 1,
    radius = ~sqrt(Pop) * 30, popup = ~City


Rectangles are added using the addRectangles() function. It takes lng1, lng2, lat1, and lat2 vector arguments that define the corners of the rectangles. These arguments are always required; the rectangle geometry cannot be inferred from the data object.

leaflet() %>% addTiles() %>%
    lng1=-118.456554, lat1=34.078039,
    lng2=-118.436383, lat2=34.062717,
    fillColor = "transparent"

The "leaflet" R package is copyright © 2014-2015 RStudio, Inc.
The Leaflet JavaScript library is © 2010–2014 Vladimir Agafonkin, 2010–2011 CloudMade.
Maps © OpenStreetMap contributors unless otherwise noted.

Fork me on GitHub