Skip to content

Commit 15ff835

Browse files
Merge pull request plotly#43 from plotly/492-announcement
update docs for 4.9.2 announcement
2 parents b479538 + e9a45d3 commit 15ff835

4 files changed

+255
-7
lines changed

r/2015-07-30-box-plots.Rmd

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,53 @@ fig <- fig %>% add_trace(y = ~rnorm(50, 1))
2626
fig
2727
```
2828

29+
### Choosing The Algorithm For Computing Quartiles
30+
31+
By default, quartiles for box plots are computed using the **linear** method (for more about linear interpolation, see #10 listed on http://www.amstat.org/publications/jse/v14n3/langford.html and https://en.wikipedia.org/wiki/Quartile for more details).
32+
33+
However, you can also choose to use an exclusive or an inclusive algorithm to compute quartiles.
34+
35+
The **exclusive** algorithm uses the median to divide the ordered dataset into two halves. If the sample is odd, it does not include the median in either half. Q1 is then the median of the lower half and Q3 is the median of the upper half.
36+
37+
The **inclusive** algorithm also uses the median to divide the ordered dataset into two halves, but if the sample is odd, it includes the median in both halves. Q1 is then the median of the lower half and Q3 the median of the upper half.
38+
39+
```{r}
40+
library(plotly)
41+
fig <- plot_ly(y = list(1,2,3,4,5), type = "box", quartilemethod="exclusive") # or "inclusive", or "linear" by default
42+
43+
fig
44+
```
45+
46+
### Modifying The Algorithm For Computing Quartiles
47+
48+
For an explanation of how each algorithm works, see [Choosing The Algorithm For Computing Quartiles](https://plotly.com/r/box-plots/#choosing-the-algorithm-for-computing-quartiles)
49+
50+
```{r}
51+
library(plotly)
52+
fig <- plot_ly(y = list(1,2,3,4,5), type = "box", quartilemethod="linear", name="Linear Quartile Mode")
53+
fig <- fig %>% add_trace(y = list(1,2,3,4,5), quartilemethod="inclusive", name="Inclusive Quartile Mode")
54+
fig <- fig %>% add_trace(y = list(1,2,3,4,5), quartilemethod="exclusive", name="Exclusive Quartile Mode")
55+
fig <- fig %>% layout(title = "Modifying The Algorithm For Computing Quartiles")
56+
57+
fig
58+
```
59+
60+
### Box Plot With Precomputed Quartiles
61+
62+
You can specify precomputed quartile attributes rather than using a built-in quartile computation algorithm.
63+
64+
This could be useful if you have already pre-computed those values or if you need to use a different algorithm than the ones provided.
65+
66+
```{r}
67+
library(plotly)
68+
fig <- plot_ly(y = list(1,2,3,4,5,6,7,8,9), type = "box", q1=list(1, 2, 3), median=list(4, 5, 6),
69+
q3=list(7, 8, 9 ), lowerfence=list(-1, 0, 1),
70+
upperfence=list(5, 6, 7), mean=list(2.2, 2.8, 3.2 ),
71+
sd=list(0.2, 0.4, 0.6), notchspan=list(0.2, 0.4, 0.6))
72+
73+
fig
74+
```
75+
2976
### Horizontal Boxplot
3077

3178
```{r}

r/2015-07-30-choropleth.Rmd

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,168 @@ 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_ly` 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_ly`: 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 choropleth traces.
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+
)
88+
)
89+
fig <- fig %>% colorbar(title = "Unemployment Rate (%)")
90+
fig <- fig %>% layout(
91+
title = "2016 US Unemployment by County"
92+
)
93+
94+
fig <- fig %>% layout(
95+
geo = g
96+
)
97+
98+
fig
99+
```
100+
**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.
101+
102+
### Indexing by GeoJSON Properties
103+
104+
If the GeoJSON you are using either does not have an `id` field or you wish you use one of the keys in the `properties` field, you may use the `featureidkey` parameter to specify where to match the values of locations.
105+
106+
In the following GeoJSON object/data-file pairing, the values of `properties.district` match the values of the `district` column:
107+
```{r}
108+
library(plotly)
109+
library(rjson)
110+
111+
url <- 'https://raw.githubusercontent.com/plotly/datasets/master/election.geojson'
112+
geojson <- rjson::fromJSON(file=url)
113+
url2<- "https://raw.githubusercontent.com/plotly/datasets/master/election.csv"
114+
df <- read.csv(url2)
115+
g <- list(
116+
fitbounds = "locations",
117+
visible = FALSE
118+
)
119+
fig <- plot_ly()
120+
fig <- fig %>% add_trace(
121+
type="choropleth",
122+
geojson=geojson,
123+
locations=df$district,
124+
z=df$Bergeron,
125+
colorscale="Viridis",
126+
featureidkey="properties.district"
127+
)
128+
fig <- fig %>% layout(
129+
geo = g
130+
)
131+
fig <- fig %>% colorbar(title = "Bergeron Votes")
132+
fig <- fig %>% layout(
133+
title = "2013 Montreal Election"
134+
)
135+
fig
136+
137+
138+
```
139+
### Using Built-in Country and State Geometries
140+
Plotly comes with two built-in geometries which do not require an external GeoJSON file:
141+
142+
1. USA States
143+
2. Countries as defined in the Natural Earth dataset.
144+
145+
**Note and disclaimer:** cultural (as opposed to physical) features are by definition subject to change, debate and dispute. Plotly includes data from Natural Earth "as-is" and defers to the [Natural Earth policy regarding disputed borders](https://www.naturalearthdata.com/downloads/50m-cultural-vectors/50m-admin-0-countries-2/) which read:
146+
147+
> Natural Earth Vector draws boundaries of countries according to defacto status. We show who actually controls the situation on the ground.
148+
149+
To use the built-in countries geometry, provide `locations` as [three-letter ISO country codes](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3).
150+
```{r}
151+
library(plotly)
152+
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv")
153+
df
154+
155+
fig <- plot_ly(df, type='choropleth', locations=df$CODE, z=df$GDP..BILLIONS., text=df$COUNTRY, colorscale="Blues")
156+
157+
fig
158+
```
159+
160+
To use the USA States geometry, set `locationmode='USA-states'` and provide `locations` as two-letter state abbreviations:
161+
```{r}
162+
library(plotly)
163+
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
164+
df$hover <- with(df, paste(state, '<br>', "Beef", beef, "Dairy", dairy, "<br>",
165+
"Fruits", total.fruits, "Veggies", total.veggies,
166+
"<br>", "Wheat", wheat, "Corn", corn))
167+
168+
fig <- plot_geo(df, locationmode = 'USA-states')
169+
fig <- fig %>% add_trace(
170+
z = ~total.exports, text = ~hover, locations = ~code,
171+
color = ~total.exports, colors = 'Purples'
172+
)
173+
fig <- fig %>% colorbar(title = "Millions USD")
174+
fig <- fig %>% layout(
175+
title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)'
176+
)
177+
178+
fig
179+
```
180+
181+
### Customize choropleth chart
21182
```{r}
22183
library(plotly)
23184
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
@@ -48,6 +209,7 @@ fig <- fig %>% layout(
48209
fig
49210
```
50211

212+
**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.
51213

52214
### World Choropleth Map
53215

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

Lines changed: 39 additions & 3 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

@@ -56,7 +56,7 @@ Here we load unemployment data by county, also indexed by [FIPS code](https://en
5656
df = read.csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", header = T, colClasses = c("fips"="character"))
5757
head(df)
5858
```
59-
### Choropleth map using carto base map (no token needed)
59+
### Mapbox Choropleth Map Using GeoJSON
6060

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

@@ -90,6 +90,42 @@ fig <- fig %>% layout(
9090
)
9191
fig
9292
```
93+
94+
### Mapbox Choropleth Map Using GeoJSON with `featureidkey`
95+
If the GeoJSON you are using either does not have an `id` field or you wish you use one of the keys in the `properties` field, you may use the `featureidkey` parameter to specify where to match the values of locations.
96+
97+
In the following GeoJSON object/data-file pairing, the values of `properties.district` match the values of the `district` column:
98+
```{r}
99+
library(plotly)
100+
library(rjson)
101+
102+
url <- 'https://raw.githubusercontent.com/plotly/datasets/master/election.geojson'
103+
geojson <- rjson::fromJSON(file=url)
104+
url2<- "https://raw.githubusercontent.com/plotly/datasets/master/election.csv"
105+
df <- read.csv(url2)
106+
g <- list(
107+
fitbounds = "locations",
108+
visible = FALSE
109+
)
110+
fig <- plot_ly()
111+
fig <- fig %>% add_trace(
112+
type="choroplethmapbox",
113+
geojson=geojson,
114+
locations=df$district,
115+
z=df$Bergeron,
116+
colorscale="Viridis",
117+
featureidkey="properties.district"
118+
)
119+
fig <- fig %>% colorbar(title = "Bergeron Votes")
120+
fig <- fig %>% layout(
121+
mapbox=list(
122+
style="carto-positron",
123+
zoom =9,
124+
center=list(lon=-73.7073, lat=45.5517))
125+
)
126+
fig
127+
```
128+
93129
#### Mapbox Light base map: free token needed
94130

95131
```{r}

r/2020-02-25-imshow.Rmd renamed to r/2020-02-25-images.Rmd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
name: Imshow
2+
name: Display Image Data
33
description: How to display image data in Python with R.
44
display_as: scientific
55
layout: base
@@ -8,7 +8,8 @@ order: 15
88
output:
99
html_document:
1010
keep_md: true
11-
permalink: r/imshow/
11+
permalink: r/displaying-images/
12+
redirect_from: r/imshow/
1213
thumbnail: thumbnail/imshow.jpg
1314
---
1415

@@ -22,6 +23,7 @@ This tutorial shows how to display and explore image data. If you would like ins
2223
Note that `Image` trace only accepts multichannel images. For single images, use [`Heatmap`](https://plotly.com/r/heatmaps/). `Image` trace is different from the `layout.Image` class, which can be used for adding background images or logos to figures.
2324
```{r}
2425
library(plotly)
26+
library(EBImage)
2527
2628
img_rgb = list(list(c(255, 0, 0),c(0, 255, 0),c(0, 0, 255)),
2729
list(c(0,255, 0),c(0, 0, 255),c(255, 0, 0)))
@@ -75,11 +77,12 @@ fig
7577
```{r}
7678
library(plotly)
7779
library(EBImage)
80+
7881
img = readImage('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg')
7982
8083
fig <- plot_ly(type="image", z=img*250)
8184
fig <- fig %>% add_trace(
82-
type='bar', y=c(50, 60), x=c(40, 50),
85+
type='scatter', y=c(50, 60), x=c(40, 50),
8386
marker=list(color='pink', size=10))
8487
fig
8588
```

0 commit comments

Comments
 (0)