Skip to content

Instantly share code, notes, and snippets.

@MonkmanMH
Created April 19, 2020 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MonkmanMH/04f23575b8bbd6fdae22e9734867b164 to your computer and use it in GitHub Desktop.
Save MonkmanMH/04f23575b8bbd6fdae22e9734867b164 to your computer and use it in GitHub Desktop.
geom_bar vs geom_col
---
title: "geom_col vs geom_bar"
author: "Martin Monkman"
date: "2020/04/19"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
Gina Reynolds tweeted this:
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Here, <a href="https://t.co/px20P5hDI7">https://t.co/px20P5hDI7</a>, you can get a sense of how geom_bar and geom_col are different, and what&#39;s geom_area. I&#39;ve been wanting to do some work on &quot;stat&quot;... NHL (hockey) actually presents some interesting challenge/question because of year canceled! <a href="https://t.co/kkJyI2aSbb">pic.twitter.com/kkJyI2aSbb</a></p>&mdash; Gina Reynolds (@EvaMaeRey) <a href="https://twitter.com/EvaMaeRey/status/1251586868841336832?ref_src=twsrc%5Etfw">April 18, 2020</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
And then replied with this:
<blockquote class="twitter-tweet"><p lang="en" dir="ltr"><a href="https://twitter.com/andrewheiss?ref_src=twsrc%5Etfw">@andrewheiss</a> <a href="https://twitter.com/CedScherer?ref_src=twsrc%5Etfw">@CedScherer</a> can you articulate why aes(fill = season) doesn&#39;t &quot;do&quot; anything in the geom_bar example (follows if you click through above link). I&#39;m having trouble putting my finger on it.</p>&mdash; Gina Reynolds (@EvaMaeRey) <a href="https://twitter.com/EvaMaeRey/status/1251587737385230336?ref_src=twsrc%5Etfw">April 18, 2020</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
I thought I'd play around a bit with some of the other geoms and variables to try to understand what's happening.
## Original code
```{r data_read}
readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-03/game_goals.csv') ->
game_goals
```
### Gina's original plots
```{r plot_col}
game_goals %>%
count(season) %>%
full_join(tibble(season = 1980:2020)) %>%
ggplot() +
aes(x = season) +
aes(y = n) +
geom_col() + # stat is identity
geom_point() + # stat is identity
geom_line() + # stat is identity
geom_area(alpha = .2, # stat is identity
fill = "magenta") +
aes(fill = season) +
aes(xend = season) +
aes(yend = 0) +
geom_segment(linetype = "dotted")
#->
#you_aggregate_then_plot
```
```{r plot_bar}
game_goals %>%
ggplot() +
aes(x = season) +
geom_bar() + # a version of geom column
geom_point(stat = "count") +
geom_line(stat = "count") +
geom_area(stat = "count",
alpha = .2,
fill = "magenta") +
aes(fill = season) # not completely grasping why fill cant work here
```
## tests
(Note: simplified code with just geom_col and geom_bar)
```{r test_col}
game_goals %>%
count(season) %>%
full_join(tibble(season = 1980:2020)) %>%
ggplot() +
aes(x = season) +
aes(y = n) + # need to specify a y-axis variable
geom_col() +
aes(fill = season)
```
```{r test_bar}
game_goals %>%
ggplot() +
aes(x = season) +
geom_bar()
```
### Try other variables
```{r test_bar}
game_goals %>%
ggplot() +
aes(x = season, fill = team) + # add `fill = team`
geom_bar()
game_goals %>%
ggplot() +
aes(x = season) +
geom_bar() +
aes(fill = team) # add `fill = team`
```
```{r test_smooth}
# geom_col -- works
game_goals %>%
count(season) %>%
full_join(tibble(season = 1980:2020)) %>%
ggplot() +
aes(x = season) +
aes(y = n) + # need to specify a y-axis variable
geom_col() +
geom_smooth()
# geom_bar -- fails
game_goals %>%
ggplot() +
aes(x = season) +
geom_bar() +
geom_smooth() # "Error: stat_smooth requires the following missing aesthetics: y"
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment