|
| 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