-
Notifications
You must be signed in to change notification settings - Fork 51
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4600bc2
choropleth mapbox
25a83f4
fixup path for mapbox_token
5b8a6ce
changing the order
b7a99a0
refactoring for the sake of consistency
90a6fa0
fix ci
0d486f0
mapbox token
e3789dc
minor revision
ddbe4b2
fix ci
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.