Thanks to visit codestin.com
Credit goes to github.com

Skip to content

choropleth mapbox #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 0 additions & 153 deletions r/2017-04-12-county-level-choropleth.Rmd

This file was deleted.

132 changes: 132 additions & 0 deletions r/2020-01-30-choropleth-rmapbox.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
---
description: How to make a Mapbox Choropleth Map of US Counties in R with Plotly.
display_as: maps
language: r
layout: base
name: Choropleth mapbox
order: 8
output:
html_document:
keep_md: true
page_type: u-guide
permalink: r/mapbox-county-choropleth/
thumbnail: thumbnail/mapbox-choropleth.png
redirect_from: r/county-level-choropleth/
---

```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
```

A [Choropleth Map](https://en.wikipedia.org/wiki/Choropleth_map) is a map composed of colored polygons. It is used to represent spatial variations of a quantity. This page documents how to build **tile-map** choropleth maps, but you can also build [**outline** choropleth maps using our non-Mapbox trace types](/r/choropleth-maps).

Below we show how to create Choropleth Maps using Plotly `Choroplethmapbox` graph object.

### Mapbox Access Token and Base Map Configuration

To plot on Mapbox maps with Plotly you *may* need a Mapbox account and a public [Mapbox Access Token](https://www.mapbox.com/studio). See our [Mapbox Map Layers](/r/mapbox-layers/) documentation for more information. If you're using a Chart Studio Enterprise server, please see additional instructions [here](https://help.plot.ly/mapbox-atlas).

### Introduction: main parameters for choropleth tile maps

Making choropleth Mapbox maps requires two main types of input:

1. GeoJSON-formatted geometry information where each feature has either an `id` field or some identifying value in `properties`.
2. A list of values indexed by feature identifier.

The GeoJSON data is passed to the `geojson` argument, and the data is passed into the `color` argument of `px.choropleth_mapbox` (`z` if using `graph_objects`), in the same order as the IDs are passed into the `location` argument.

**Note** the `geojson` attribute can also be the URL to a GeoJSON file, which can speed up map rendering in certain cases.

#### GeoJSON with `feature.id`

Here we load a GeoJSON file containing the geometry information for US counties, where `feature.id` is a [FIPS code](https://en.wikipedia.org/wiki/FIPS_county_code).

```{r}
library(rjson)
url = 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
json_file <- rjson::fromJSON(file=url)
json_file$features[[1]]$id
```

### Data indexed by `id`

Here we load unemployment data by county, also indexed by [FIPS code](https://en.wikipedia.org/wiki/FIPS_county_code).

```{r}
df = read.csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", header = T, colClasses = c("fips"="character"))
head(df)
```
### Choropleth map using carto base map (no token needed)

With `choroplethmapbox`, each row of the DataFrame is represented as a region of the choropleth.

```{r}
library(rjson)
library(plotly)

url <- 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
counties <- rjson::fromJSON(file=url)
url2<- "https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv"
df <- read.csv(url2, colClasses=c(fips="character"))
fig <- plot_ly()
fig <- fig %>% add_trace(
type="choroplethmapbox",
geojson=counties,
locations=df$fips,
z=df$unemp,
colorscale="Viridis",
zmin=0,
zmax=12,
marker=list(line=list(
width=0),
opacity=0.5
)
)
fig <- fig %>% layout(
mapbox=list(
style="carto-positron",
zoom =2,
center=list(lon= -95.71, lat=37.09))
)
fig
```
#### Mapbox Light base map: free token needed

```{r}
library(rjson)
library(plotly)

mapboxToken <- paste(readLines("../.mapbox_token"), collapse="") # You need your own token
Sys.setenv("MAPBOX_TOKEN" = mapboxToken) # for Orca

url <- 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
counties <- rjson::fromJSON(file = url)
url2<- "https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv"
df <- read.csv(url2,colClasses = c(fips = "character"))

fig <- plot_ly()
fig <- fig %>% add_trace(
type = "choroplethmapbox",
geojson = counties,
locations = df$fips,
z=df$unemp,
colorscale="Viridis",
zmin=0,
zmax=12,
marker=list(line = list(
width = 0),
opacity=0.5
)
)
fig <- fig %>% layout(
mapbox = list(
style = "light",
zoom =3,
center = list(lon = -95.7129, lat = 37.0902))
)
fig <- fig %>% config(mapboxAccessToken = Sys.getenv("MAPBOX_TOKEN"))
fig
```
#Reference

See [https://plot.ly/r/reference/#scattermapbox](https://plot.ly/r/reference/#choroplethmapbox) for more information and options!