Skip to content

Commit 3e76187

Browse files
author
Joseph Damiba
committed
add doc for choropleth map with geojson
1 parent 5998974 commit 3e76187

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

announcement.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Check out the image below to see how much nicer this makes everything look.
2020

2121
#### GeoJson Choropleth Maps and Mapping Improvements
2222

23-
Choropleth maps now accept a `geojson` argument. This means that you can now supply your own geometry information to non-tile-map choropleth maps, instead of relying on Plotly’s built-in country and state maps. Some other new mapping features include:
23+
Choropleth maps now accept a [`geojson`](https://plotly.com/r/mapbox-county-choropleth/#choropleth-map-with-geojson) argument. This means that you can now supply your own geometry information to non-tile-map choropleth maps, instead of relying on Plotly’s built-in country and state maps. Some other new mapping features include:
2424

2525
- New [`featureidkey`](https://plotly.com/r/mapbox-county-choropleth/#geojson-with-featureid) attribute to let you use GeoJSON objects whose identifiers are in the properties object of a feature, rather than always and only using the `id` field. (This was added to the Mapbox choropleth trace as well.)
2626

r/2015-07-30-choropleth.Rmd

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,93 @@ thumbnail: thumbnail/choropleth.jpg
1717
```{r, echo = FALSE, message=FALSE}
1818
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
1919
```
20-
# Choropleth Maps in R
20+
21+
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 **outline** choropleth maps, but you can also build [choropleth **tile maps** using our Mapbox trace types](/r/mapbox-county-choropleth).
22+
23+
### Base Map Configuration
24+
25+
Plotly figures made with `plot_geo` have a `layout.geo` object which can be used to control the appearance of the base map onto which data is plotted.
26+
27+
### Introduction: main parameters for choropleth outline maps
28+
29+
Making choropleth maps requires two main types of input:
30+
31+
1. Geometry information:
32+
1. This can either be a supplied GeoJSON file where each feature has either an `id` field or some identifying value in `properties`; or
33+
2. one of the built-in geometries within `plot_geo`: US states and world countries (see below)
34+
2. A list of values indexed by feature identifier.
35+
36+
The GeoJSON data is passed to the `geojson` argument, and the data is passed into the `z` argument of `plot_geo`.
37+
38+
**Note** the `geojson` attribute can also be the URL to a GeoJSON file, which can speed up map rendering in certain cases.
39+
40+
### GeoJSON with `feature.id`
41+
42+
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).
43+
44+
```{r}
45+
library(plotly)
46+
library(rjson)
47+
48+
data <- fromJSON(file="https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json")
49+
data$features[[1]]
50+
```
51+
52+
#### Data indexed by `id`
53+
54+
Here we load unemployment data by county, also indexed by [FIPS code](https://en.wikipedia.org/wiki/FIPS_county_code).
55+
56+
```{r}
57+
df = read.csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", header = T, colClasses = c("fips"="character"))
58+
head(df)
59+
```
60+
61+
### Choropleth Map Using GeoJSON
62+
```{r}
63+
library(plotly)
64+
library(rjson)
65+
66+
url <- 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
67+
counties <- rjson::fromJSON(file=url)
68+
url2<- "https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv"
69+
df <- read.csv(url2, colClasses=c(fips="character"))
70+
g <- list(
71+
scope = 'usa',
72+
projection = list(type = 'albers usa'),
73+
showlakes = TRUE,
74+
lakecolor = toRGB('white')
75+
)
76+
fig <- plot_ly()
77+
fig <- fig %>% add_trace(
78+
type="choropleth",
79+
geojson=counties,
80+
locations=df$fips,
81+
z=df$unemp,
82+
colorscale="Viridis",
83+
zmin=0,
84+
zmax=12,
85+
marker=list(line=list(
86+
width=0),
87+
opacity=0.5
88+
)
89+
)
90+
fig <- fig %>% colorbar(title = "Unemployment Rate (%)")
91+
fig <- fig %>% layout(
92+
title = "2016 US Unemployment by State"
93+
)
94+
95+
fig <- fig %>% layout(
96+
mapbox=list(
97+
zoom =3,
98+
center = list(lon = -95.7129, lat = 37.0902)),
99+
geo = g
100+
)
101+
102+
fig
103+
```
104+
**Note** In this example we set `layout.geo.scope` to usa to automatically configure the map to display USA-centric data in an appropriate projection.
105+
106+
### Customize choropleth chart
21107
```{r}
22108
library(plotly)
23109
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
@@ -48,6 +134,7 @@ fig <- fig %>% layout(
48134
fig
49135
```
50136

137+
**Note** In this example we set `layout.geo.scope` to `usa` to automatically configure the map to display USA-centric data in an appropriate projection.
51138

52139
### World Choropleth Map
53140

r/2020-01-30-choropleth-rmapbox.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: How to make a Mapbox Choropleth Map of US Counties in R with Plotly
33
display_as: maps
44
language: r
55
layout: base
6-
name: Choropleth mapbox
6+
name: Mapbox Choropleth Maps
77
order: 8
88
output:
99
html_document:
@@ -33,7 +33,7 @@ Making choropleth Mapbox maps requires two main types of input:
3333
1. GeoJSON-formatted geometry information where each feature has either an `id` field or some identifying value in `properties`.
3434
2. A list of values indexed by feature identifier.
3535

36-
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.
36+
The GeoJSON data is passed to the `geojson` argument, and the data is passed into the `z` argument of `plot_geo`.
3737

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

0 commit comments

Comments
 (0)