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

Skip to content

Commit d502062

Browse files
author
Joseph Damiba
committed
adding more posts to docs repo
1 parent b2b6e2a commit d502062

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+9760
-0
lines changed

r/2015-07-30-axes.Rmd

Lines changed: 417 additions & 0 deletions
Large diffs are not rendered by default.

r/2015-07-30-bubble-maps.Rmd

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
name: Bubble Maps
3+
permalink: r/bubble-maps/
4+
description: How to make a bubble chart and map in R.
5+
layout: base
6+
thumbnail: thumbnail/bubble-map.jpg
7+
language: r
8+
page_type: example_index
9+
display_as: maps
10+
order: 5
11+
output:
12+
html_document:
13+
keep_md: true
14+
---
15+
16+
```{r, echo = FALSE, message=FALSE}
17+
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
18+
```
19+
### New to Plotly?
20+
21+
Plotly's R library is free and open source!<br>
22+
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
23+
You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.<br>
24+
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!
25+
26+
### Version Check
27+
28+
Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
29+
Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version.
30+
```{r}
31+
library(plotly)
32+
packageVersion('plotly')
33+
```
34+
35+
#### United States Bubble Map
36+
37+
```{r}
38+
library(plotly)
39+
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv')
40+
41+
df$q <- with(df, cut(pop, quantile(pop)))
42+
levels(df$q) <- paste(c("1st", "2nd", "3rd", "4th", "5th"), "Quantile")
43+
df$q <- as.ordered(df$q)
44+
45+
g <- list(
46+
scope = 'usa',
47+
projection = list(type = 'albers usa'),
48+
showland = TRUE,
49+
landcolor = toRGB("gray85"),
50+
subunitwidth = 1,
51+
countrywidth = 1,
52+
subunitcolor = toRGB("white"),
53+
countrycolor = toRGB("white")
54+
)
55+
56+
p <- plot_geo(df, locationmode = 'USA-states', sizes = c(1, 250)) %>%
57+
add_markers(
58+
x = ~lon, y = ~lat, size = ~pop, color = ~q, hoverinfo = "text",
59+
text = ~paste(df$name, "<br />", df$pop/1e6, " million")
60+
) %>%
61+
layout(title = '2014 US city populations<br>(Click legend to toggle)', geo = g)
62+
63+
p
64+
```

r/2015-07-30-choropleth.Rmd

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
name: Choropleth Maps
3+
permalink: r/choropleth-maps/
4+
description: How to make a choropleth map in R. A choropleth map shades geographic regions by value.
5+
layout: base
6+
thumbnail: thumbnail/choropleth.jpg
7+
language: r
8+
page_type: example_index
9+
display_as: maps
10+
order: 0
11+
output:
12+
html_document:
13+
keep_md: true
14+
---
15+
16+
```{r, echo = FALSE, message=FALSE}
17+
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
18+
```
19+
### New to Plotly?
20+
21+
Plotly's R library is free and open source!<br>
22+
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
23+
You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.<br>
24+
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!
25+
26+
### Version Check
27+
28+
Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
29+
Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version.
30+
```{r}
31+
library(plotly)
32+
packageVersion('plotly')
33+
```
34+
35+
# Choropleth Maps in R
36+
```{r}
37+
library(plotly)
38+
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
39+
df$hover <- with(df, paste(state, '<br>', "Beef", beef, "Dairy", dairy, "<br>",
40+
"Fruits", total.fruits, "Veggies", total.veggies,
41+
"<br>", "Wheat", wheat, "Corn", corn))
42+
# give state boundaries a white border
43+
l <- list(color = toRGB("white"), width = 2)
44+
# specify some map projection/options
45+
g <- list(
46+
scope = 'usa',
47+
projection = list(type = 'albers usa'),
48+
showlakes = TRUE,
49+
lakecolor = toRGB('white')
50+
)
51+
52+
p <- plot_geo(df, locationmode = 'USA-states') %>%
53+
add_trace(
54+
z = ~total.exports, text = ~hover, locations = ~code,
55+
color = ~total.exports, colors = 'Purples'
56+
) %>%
57+
colorbar(title = "Millions USD") %>%
58+
layout(
59+
title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
60+
geo = g
61+
)
62+
63+
p
64+
```
65+
66+
67+
### World Choropleth Map
68+
69+
```{r}
70+
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')
71+
72+
# light grey boundaries
73+
l <- list(color = toRGB("grey"), width = 0.5)
74+
75+
# specify map projection/options
76+
g <- list(
77+
showframe = FALSE,
78+
showcoastlines = FALSE,
79+
projection = list(type = 'Mercator')
80+
)
81+
82+
p <- plot_geo(df) %>%
83+
add_trace(
84+
z = ~GDP..BILLIONS., color = ~GDP..BILLIONS., colors = 'Blues',
85+
text = ~COUNTRY, locations = ~CODE, marker = list(line = l)
86+
) %>%
87+
colorbar(title = 'GDP Billions US$', tickprefix = '$') %>%
88+
layout(
89+
title = '2014 Global GDP<br>Source:<a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">CIA World Factbook</a>',
90+
geo = g
91+
)
92+
93+
p
94+
```
95+
96+
### Choropleth Inset Map
97+
98+
```{r}
99+
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_ebola.csv')
100+
# restrict from June to September
101+
df <- subset(df, Month %in% 6:9)
102+
# ordered factor variable with month abbreviations
103+
df$abbrev <- ordered(month.abb[df$Month], levels = month.abb[6:9])
104+
# September totals
105+
df9 <- subset(df, Month == 9)
106+
107+
# common plot options
108+
g <- list(
109+
scope = 'africa',
110+
showframe = F,
111+
showland = T,
112+
landcolor = toRGB("grey90")
113+
)
114+
115+
g1 <- c(
116+
g,
117+
resolution = 50,
118+
showcoastlines = T,
119+
countrycolor = toRGB("white"),
120+
coastlinecolor = toRGB("white"),
121+
projection = list(type = 'Mercator'),
122+
list(lonaxis = list(range = c(-15, -5))),
123+
list(lataxis = list(range = c(0, 12))),
124+
list(domain = list(x = c(0, 1), y = c(0, 1)))
125+
)
126+
127+
g2 <- c(
128+
g,
129+
showcountries = F,
130+
bgcolor = toRGB("white", alpha = 0),
131+
list(domain = list(x = c(0, .6), y = c(0, .6)))
132+
)
133+
134+
p <- df %>%
135+
plot_geo(
136+
locationmode = 'country names', sizes = c(1, 600), color = I("black")
137+
) %>%
138+
add_markers(
139+
y = ~Lat, x = ~Lon, locations = ~Country,
140+
size = ~Value, color = ~abbrev, text = ~paste(Value, "cases")
141+
) %>%
142+
add_text(
143+
x = 21.0936, y = 7.1881, text = 'Africa', showlegend = F, geo = "geo2"
144+
) %>%
145+
add_trace(
146+
data = df9, z = ~Month, locations = ~Country,
147+
showscale = F, geo = "geo2"
148+
) %>%
149+
layout(
150+
title = 'Ebola cases reported by month in West Africa 2014<br> Source: <a href="https://data.hdx.rwlabs.org/dataset/rowca-ebola-cases">HDX</a>',
151+
geo = g1, geo2 = g2
152+
)
153+
154+
p
155+
```

0 commit comments

Comments
 (0)