"I'll never understand"
"I just don't get programming"
"I'm not a maths person"
"I'll never understand"
"I just don't get programming"
"I'm not a maths person"
"I understand more than I did yesterday"
"I can learn how to program"
"Compared to this last week, I've learnt quite a bit!"
The case notifications table From WHO.
Data is tidied here, with only counts for Australia.
tb_au
## # A tibble: 192 x 6## country iso3 year count gender age ## <chr> <chr> <dbl> <dbl> <chr> <chr>## 1 Australia AUS 1997 8 m 1524 ## 2 Australia AUS 1998 11 m 1524 ## 3 Australia AUS 1999 13 m 1524 ## 4 Australia AUS 2000 16 m 1524 ## 5 Australia AUS 2001 23 m 1524 ## 6 Australia AUS 2002 15 m 1524 ## 7 Australia AUS 2003 14 m 1524 ## 8 Australia AUS 2004 18 m 1524 ## 9 Australia AUS 2005 32 m 1524 ## 10 Australia AUS 2006 33 m 1524 ## # … with 182 more rows
ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_bar(stat = "identity", position = "fill") + facet_grid(~ age) + scale_fill_brewer(palette="Dark2")
100% charts, is what excel names these beasts. What do we learn?
"The simple graph has brought more information to the data analyst’s mind than any other device." — John Tukey
gg
in "ggplot2" stands for Grammar of Graphics† Source: BloggoType
library(ggplot2)ggplot(tb_au)
library(ggplot2)ggplot(tb_au, aes(x = year, y = count))
library(ggplot2)ggplot(tb_au, aes(x = year, y = count)) + geom_point()
country | iso3 | year | count | gender | age |
---|---|---|---|---|---|
Australia | AUS | 1997 | 8 | m | 1524 |
Australia | AUS | 1998 | 11 | m | 1524 |
Australia | AUS | 1999 | 13 | m | 1524 |
Australia | AUS | 2000 | 16 | m | 1524 |
Australia | AUS | 2001 | 23 | m | 1524 |
Australia | AUS | 2002 | 15 | m | 1524 |
Australia | AUS | 2003 | 14 | m | 1524 |
Australia | AUS | 2004 | 18 | m | 1524 |
Australia | AUS | 2005 | 32 | m | 1524 |
Australia | AUS | 2006 | 33 | m | 1524 |
library(ggplot2)ggplot(tb_au, aes(x = year, y = count)) + geom_col()
library(ggplot2)ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_col()
library(ggplot2)ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_col(position = "fill")
library(ggplot2)ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_col(position = "fill") + scale_fill_brewer( palette = "Dark2" )
library(ggplot2)ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_col(position = "fill") + scale_fill_brewer( palette = "Dark2" ) + facet_wrap(~ age)
tb_au
geom_col
position = "fill"
option in geom_bar
sets the heights of the bars to be all at 100%. It ignores counts, and emphasizes the proportion of males and females.ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_bar(stat = "identity", position = "fill") + facet_grid(~ age) + scale_fill_brewer(palette="Dark2")
ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_bar(stat = "identity", position = "fill") + facet_grid(~ age) + scale_fill_brewer(palette="Dark2")
What do we learn
100% charts, is what excel names these beasts. What do we learn?
ggplot()
is the main functionggplot(data = [dataset], mapping = aes(x = [x-variable], y = [y-variable])) + geom_xxx() + other options
library(tidyverse)
Let's look at some more options to emphasise different features
ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_col(position = "fill") + scale_fill_brewer( palette = "Dark2" ) + facet_wrap(~ age)
ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_col(position = "fill") + scale_fill_brewer( palette = "Dark2") + facet_grid(~ age)
ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_col() + scale_fill_brewer( palette = "Dark2") + facet_grid(~ age)
, position = "fill"
was removedggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_col(position = "dodge") + scale_fill_brewer(palette = "Dark2") + facet_grid(~ age)
, position="dodge"
is used in geom_col
ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_col() + scale_fill_brewer(palette = "Dark2") + facet_grid(gender ~ age)
facet_grid(gender ~ age) +
faceted by gender as well as agefacet_grid
vs facet_wrap
ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_col() + scale_fill_brewer(palette="Dark2") + facet_grid(gender ~ age) + coord_polar() + theme(axis.text = element_blank())
coord_polar() +
plot is made in polar coordinates, rather than the default Cartesian coordinatesggplot(tb_au, aes(x = 1, y = count, fill = factor(year))) + geom_col(position = "fill") + facet_grid(gender ~ age)
ggplot(tb_au, aes(x = 1, y = count, fill = factor(year))) + geom_col(position = "fill") + facet_grid(gender ~ age) + coord_polar(theta = "y") + theme(axis.text = element_blank())
coord_polar(theta="y")
is using the y variable to do the angles for the polar coordinates to give a pie chart.The various looks of David Bowie
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |