Skip to content

Commit 630ec4f

Browse files
authored
Merge pull request #1449 from plotly/geom_count
geom_count
2 parents d5d0200 + cb67ac5 commit 630ec4f

File tree

2 files changed

+350
-0
lines changed

2 files changed

+350
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
---
2+
title: geom_count | Examples | Plotly
3+
name: geom_count
4+
permalink: ggplot2/geom_count/
5+
description: How to make a 2-dimensional frequency graph in ggplot2 using geom_count Examples of coloured and facetted graphs.
6+
layout: base
7+
thumbnail: thumbnail/geom_count.jpg
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: statistical
12+
order: 2
13+
output:
14+
html_document:
15+
keep_md: true
16+
---
17+
18+
```{r, echo = FALSE, message=FALSE}
19+
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
20+
Sys.setenv("plotly_username"="RPlotBot")
21+
Sys.setenv("plotly_api_key"="q0lz6r5efr")
22+
```
23+
24+
### New to Plotly?
25+
26+
Plotly's R library is free and open source!<br>
27+
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
28+
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>
29+
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!
30+
31+
### Version Check
32+
33+
Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
34+
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.
35+
36+
```{r}
37+
library(plotly)
38+
packageVersion('plotly')
39+
```
40+
41+
### Basic geom\_count Plot
42+
geom\_count is a way to plot two variables that are not continuous. Here's a modified version of the nycflights13 dataset that comes with R; it shows 2013 domestic flights leaving New York's three airports. This graph maps two categorical variables: which of America's major airports it was headed to, and which major carrier was operating it.
43+
44+
It's good to show the full airport names for destinations, rather than just the airport codes. You can use aes(group = ), which doesn't modify the graph in any way but adds information to the labels.
45+
46+
```{r, results='hide'}
47+
library(plotly)
48+
flightdata <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/flightdata.csv", stringsAsFactors = FALSE)
49+
50+
p <- ggplot(flightdata, aes(y=airline, x=dest, colour = dest, group=airport)) +
51+
geom_count(alpha=0.5) +
52+
labs(title = "Flights from New York to major domestic destinations",
53+
x = "Origin and destination",
54+
y = "Airline",
55+
size = "")
56+
ggplotly(p)
57+
58+
# Create a shareable link to your chart
59+
# Set up API credentials: https://plot.ly/r/getting-started
60+
chart_link = api_create(p, filename="geom_count/basic-plot")
61+
chart_link
62+
```
63+
64+
```{r echo=FALSE}
65+
chart_link
66+
```
67+
68+
### Adding a Third Variable
69+
By using facets, we can add a third variable: which of New York's three airports it departed from. We can also colour-code by this variable.
70+
71+
```{r, results='hide'}
72+
library(plotly)
73+
flightdata <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/flightdata.csv", stringsAsFactors = FALSE)
74+
75+
p <- ggplot(flightdata, aes(y=airline, x=origin, colour=origin, group=airport)) +
76+
geom_count(alpha=0.5) +
77+
facet_grid(. ~ dest) +
78+
labs(title = "Flights from New York to major domestic destinations",
79+
x = "Origin and destination",
80+
y = "Airline",
81+
size = "")
82+
ggplotly(p)
83+
84+
# Create a shareable link to your chart
85+
# Set up API credentials: https://plot.ly/r/getting-started
86+
chart_link = api_create(p, filename="geom_count/three-variables")
87+
chart_link
88+
```
89+
90+
```{r echo=FALSE}
91+
chart_link
92+
```
93+
94+
### Customized appearance
95+
The airport labels at the bottom aren't very visible and aren't very important, since there's a colour key to the side; we can get rid of the text and ticks using theme() options. Let's also use the LaCroixColoR package to give this geom\_count chart a new colour scheme.
96+
97+
```{r, results='hide'}
98+
library(plotly)
99+
library(LaCroixColoR)
100+
flightdata <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/flightdata.csv", stringsAsFactors = FALSE)
101+
102+
p <- ggplot(flightdata, aes(y=airline, x=origin, colour=origin, group=airport)) +
103+
geom_count(alpha=0.5) +
104+
facet_grid(. ~ dest) +
105+
scale_colour_manual(values = lacroix_palette("PassionFruit", n=3)) +
106+
theme(axis.text.x = element_blank(),
107+
axis.ticks.x = element_blank()) +
108+
labs(title = "Flights from New York to major domestic destinations",
109+
x = "Origin and destination",
110+
y = "Airline",
111+
size = "")
112+
ggplotly(p)
113+
114+
# Create a shareable link to your chart
115+
# Set up API credentials: https://plot.ly/r/getting-started
116+
chart_link = api_create(p, filename="geom_count/customize-theme")
117+
chart_link
118+
```
119+
120+
```{r echo=FALSE}
121+
chart_link
122+
```
123+
124+
### geom\_count vs geom\_point
125+
Here's a comparison of geom\_count and geom\_point on the same dataset (rounded for geom\_count). Geom\_point has the advantage of allowing multiple colours on the same graph, as well as a label for each point. But even with a low alpha, there are too many overlapping points to understand what the actual distribution looks like, only a general impression.
126+
127+
```{r, results='hide'}
128+
library(plotly)
129+
library(dplyr)
130+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
131+
132+
df <- beers %>%
133+
mutate(abv = round(abv*100),
134+
ibu = round(ibu/10)*10) %>%
135+
filter(!is.na(style2))
136+
137+
p <- ggplot(df, aes(x=abv, y=ibu, colour=style2)) +
138+
geom_count(alpha=0.5) +
139+
theme(legend.position = "none") +
140+
facet_wrap(~style2)
141+
ggplotly(p)
142+
143+
# Create a shareable link to your chart
144+
# Set up API credentials: https://plot.ly/r/getting-started
145+
chart_link = api_create(p, filename="geom_count/compare-count")
146+
chart_link
147+
```
148+
149+
```{r echo=FALSE}
150+
chart_link
151+
```
152+
153+
```{r, results='hide'}
154+
library(plotly)
155+
library(dplyr)
156+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
157+
158+
df <- filter(beers, !is.na(style2))
159+
160+
p <- ggplot(df, aes(x=abv, y=ibu, colour=style2)) +
161+
geom_point(alpha=0.2, aes(text = label)) +
162+
theme(legend.position = "none") +
163+
facet_wrap(~style2) +
164+
labs(y = "bitterness (IBU)",
165+
x = "alcohol volume (ABV)",
166+
title = "Craft beers from American breweries")
167+
ggplotly(p)
168+
169+
# Create a shareable link to your chart
170+
# Set up API credentials: https://plot.ly/r/getting-started
171+
chart_link = api_create(p, filename="geom_count/compare-point")
172+
chart_link
173+
```
174+
175+
```{r echo=FALSE}
176+
chart_link
177+
```
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
title: geom_count | Examples | Plotly
3+
name: geom_count
4+
permalink: ggplot2/geom_count/
5+
description: How to make a 2-dimensional frequency graph in ggplot2 using geom_count Examples of coloured and facetted graphs.
6+
layout: base
7+
thumbnail: thumbnail/geom_count.jpg
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: statistical
12+
order: 2
13+
output:
14+
html_document:
15+
keep_md: true
16+
---
17+
18+
19+
20+
### New to Plotly?
21+
22+
Plotly's R library is free and open source!<br>
23+
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
24+
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>
25+
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!
26+
27+
### Version Check
28+
29+
Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
30+
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.
31+
32+
33+
```r
34+
library(plotly)
35+
packageVersion('plotly')
36+
```
37+
38+
```
39+
## [1] '4.8.0.9000'
40+
```
41+
42+
### Basic geom\_count Plot
43+
geom\_count is a way to plot two variables that are not continuous. Here's a modified version of the nycflights13 dataset that comes with R; it shows 2013 domestic flights leaving New York's three airports. This graph maps two categorical variables: which of America's major airports it was headed to, and which major carrier was operating it.
44+
45+
It's good to show the ful airport names for destinations, rather than just the airport codes. You can use aes(group = ), which doesn't modify the graph in any way but adds information to the labels.
46+
47+
48+
```r
49+
library(plotly)
50+
flightdata <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/flightdata.csv", stringsAsFactors = FALSE)
51+
52+
p <- ggplot(flightdata, aes(y=airline, x=dest, colour = dest, group=airport)) +
53+
geom_count(alpha=0.5) +
54+
labs(title = "Flights from New York to major domestic destinations",
55+
x = "Origin and destination",
56+
y = "Airline",
57+
size = "")
58+
ggplotly(p)
59+
60+
# Create a shareable link to your chart
61+
# Set up API credentials: https://plot.ly/r/getting-started
62+
chart_link = api_create(p, filename="geom_count/basic-plot")
63+
chart_link
64+
```
65+
66+
<iframe src="https://plot.ly/~RPlotBot/5819.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
67+
68+
### Adding a Third Variable
69+
By using facets, we can add a third variable: which of New York's three airports it departed from. We can also colour-code by this variable.
70+
71+
72+
```r
73+
library(plotly)
74+
flightdata <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/flightdata.csv", stringsAsFactors = FALSE)
75+
76+
p <- ggplot(flightdata, aes(y=airline, x=origin, colour=origin, group=airport)) +
77+
geom_count(alpha=0.5) +
78+
facet_grid(. ~ dest) +
79+
labs(title = "Flights from New York to major domestic destinations",
80+
x = "Origin and destination",
81+
y = "Airline",
82+
size = "")
83+
ggplotly(p)
84+
85+
# Create a shareable link to your chart
86+
# Set up API credentials: https://plot.ly/r/getting-started
87+
chart_link = api_create(p, filename="geom_count/three-variables")
88+
chart_link
89+
```
90+
91+
<iframe src="https://plot.ly/~RPlotBot/5821.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
92+
93+
### Customized appearance
94+
The airport labels at the bottom aren't very visible and aren't very important, since there's a colour key to the side; we can get rid of the text and ticks using theme() options. Let's also use the LaCroixColoR package to give this geom\_count chart a new colour scheme.
95+
96+
97+
```r
98+
library(plotly)
99+
library(LaCroixColoR)
100+
flightdata <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/flightdata.csv", stringsAsFactors = FALSE)
101+
102+
p <- ggplot(flightdata, aes(y=airline, x=origin, colour=origin, group=airport)) +
103+
geom_count(alpha=0.5) +
104+
facet_grid(. ~ dest) +
105+
scale_colour_manual(values = lacroix_palette("PassionFruit", n=3)) +
106+
theme(axis.text.x = element_blank(),
107+
axis.ticks.x = element_blank()) +
108+
labs(title = "Flights from New York to major domestic destinations",
109+
x = "Origin and destination",
110+
y = "Airline",
111+
size = "")
112+
ggplotly(p)
113+
114+
# Create a shareable link to your chart
115+
# Set up API credentials: https://plot.ly/r/getting-started
116+
chart_link = api_create(p, filename="geom_count/customize-theme")
117+
chart_link
118+
```
119+
120+
<iframe src="https://plot.ly/~RPlotBot/5823.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
121+
122+
### geom\_count vs geom\_point
123+
Here's a comparison of geom\_count and geom\_point on the same dataset (rounded for geom\_count). Geom\_point has the advantage of allowing multiple colours on the same graph, as well as a label for each point. But even with a low alpha, there are too many overlapping points to understand what the actual distribution looks like, only a general impression.
124+
125+
126+
```r
127+
library(plotly)
128+
library(dplyr)
129+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
130+
131+
df <- beers %>%
132+
mutate(abv = round(abv*100),
133+
ibu = round(ibu/10)*10) %>%
134+
filter(!is.na(style2))
135+
136+
p <- ggplot(df, aes(x=abv, y=ibu, colour=style2)) +
137+
geom_count(alpha=0.5) +
138+
theme(legend.position = "none") +
139+
facet_wrap(~style2)
140+
ggplotly(p)
141+
142+
# Create a shareable link to your chart
143+
# Set up API credentials: https://plot.ly/r/getting-started
144+
chart_link = api_create(p, filename="geom_count/compare-count")
145+
chart_link
146+
```
147+
148+
<iframe src="https://plot.ly/~RPlotBot/5825.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
149+
150+
151+
```r
152+
library(plotly)
153+
library(dplyr)
154+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
155+
156+
df <- filter(beers, !is.na(style2))
157+
158+
p <- ggplot(df, aes(x=abv, y=ibu, colour=style2)) +
159+
geom_point(alpha=0.2, aes(text = label)) +
160+
theme(legend.position = "none") +
161+
facet_wrap(~style2) +
162+
labs(y = "bitterness (IBU)",
163+
x = "alcohol volume (ABV)",
164+
title = "Craft beers from American breweries")
165+
ggplotly(p)
166+
167+
# Create a shareable link to your chart
168+
# Set up API credentials: https://plot.ly/r/getting-started
169+
chart_link = api_create(p, filename="geom_count/compare-point")
170+
chart_link
171+
```
172+
173+
<iframe src="https://plot.ly/~RPlotBot/5827.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

0 commit comments

Comments
 (0)