# A tibble: 3 × 2
sex n
<fct> <int>
1 female 165
2 male 168
3 <NA> 11
Data Visualisation with R
geom_bar()
with a categorical variablestat = "count"
is computing the frequencies for each category for you.geom_bar()
with a discrete numerical variablex = 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.geom_bar()
is used to create barplots with categorical variables.geom_col()
is used to create barplots with pre-computed counts.position
argument in geom_col()
can be used to adjust the position of the bars.