👩🏻💻 Emi Tanaka @ Monash University
28th November 2022 Australasian Applied Statistics Conference 2022
geom_bar() with a categorical variablestat = "count" is computing the frequencies for each category for you.geom_bar() with a discrete numerical variable
x = factor(year).geom_col()# A tibble: 3 × 2
sex n
<fct> <int>
1 female 165
2 male 168
3 <NA> 11
stat = "count" to do the counting for you and use geom_col() instead.geom_col()penguins %>%
group_by(species, sex, year) %>%
tally() %>%
ggplot(aes(year, n, fill = sex, group = year, color = species)) +
geom_col(position = "stack", linewidth = 8) +
geom_col(position = "stack", linewidth = 1, color = "black")
y are stacked on top of another.group here breaks the count in two groups and stack one on top of the other (try running the code without group = year).geom_col()penguins %>%
group_by(sex, species, year) %>%
tally() %>%
ggplot(aes(sex, n, fill = species)) +
geom_col(color = "black", position = "dodge")
x values are recalculated so that the factor levels within the same group (as determined by x) can fit.geom_col()penguins %>%
group_by(sex, species, year) %>%
tally() %>%
ggplot(aes(sex, n, fill = species, group = year)) +
geom_col(color = "black", position = "dodge2")
position = "dodge" doesn’t deal well when there is fill and group together but you can use position = "dodge2" that recalculates the x values in another way.geom_col()penguins %>%
group_by(species, sex, year) %>%
tally() %>%
ggplot(aes(sex, n, fill = species, group = year)) +
geom_col(color = "black", position = "fill")
x, then position = "fill" can be handy.coord_polar()coord_cartesian() for Cartesian coordinate systems (default)coord_flip() to flip the x and ycoord_fixed() to use a fixed aspect ratiocoord_equal() is essentially coord_fixed(ratio = 1)coord_trans() to transform the coordinate after the statistical transformationcoord_map() to use projection based on mapprojYour turn!
10:00
> Go to emitanaka.org/dataviz-workshop/exercises/
> Click Exercise 4
