create a map in r

To create a map in R, we can use the leaflet package which allows us to create interactive and customizable maps. Here's an example code to create a map of the United States with state borders:

main.r
library(leaflet)

# create a list of state borders
states <- geojsonio::geojson_read("https://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_040_00_20m.json", what = "sp")

# create a map centered at the US
m <- leaflet() %>%
  setView(lng = -96, lat = 37.8, zoom = 4) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(data = states, stroke = FALSE, fillOpacity = 0.5)

# display the map
m
405 chars
14 lines

This code first reads the state borders from a GeoJSON file and creates a list. Then we create a leaflet map centered at the US with an initial zoom level. We add a tile layer from CartoDB.Positron to give a nice background to the map. Finally, we add the state borders using the addPolygons function.

You can customize the map by changing the zoom level, the tile layer, and the colors of the polygons. The leaflet package offers many options for creating interactive and dynamic maps in R.

gistlibby LogSnag