library("tidyverse")
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ── ✔ ggplot2 3.3.2 ✔ purrr 0.3.4 ✔ tibble 3.0.3 ✔ dplyr 1.0.2 ✔ tidyr 1.1.1 ✔ stringr 1.4.0 ✔ readr 1.3.1 ✔ forcats 0.5.0 ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ── ✖ dplyr::filter() masks stats::filter() ✖ dplyr::lag() masks stats::lag()
options(repr.plot.width=4, repr.plot.height=3, repr.plot.res = 300)
library("palmerpenguins")
penguins %>% head
species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year |
---|---|---|---|---|---|---|---|
<fct> | <fct> | <dbl> | <dbl> | <int> | <int> | <fct> | <int> |
Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 |
Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 |
Adelie | Torgersen | 40.3 | 18.0 | 195 | 3250 | female | 2007 |
Adelie | Torgersen | NA | NA | NA | NA | NA | 2007 |
Adelie | Torgersen | 36.7 | 19.3 | 193 | 3450 | female | 2007 |
Adelie | Torgersen | 39.3 | 20.6 | 190 | 3650 | male | 2007 |
df_counts = penguins %>%
drop_na() %>%
group_by(species, sex) %>%
count()
df_counts
species | sex | n |
---|---|---|
<fct> | <fct> | <int> |
Adelie | female | 73 |
Adelie | male | 73 |
Chinstrap | female | 34 |
Chinstrap | male | 34 |
Gentoo | female | 58 |
Gentoo | male | 61 |
We want to visualize the number of each sex within each species?
We want to visualize the number of each sex within each species?
p = ggplot(df_counts, aes(x = species, y = n, fill = sex))
p = p + geom_col()
p
p = ggplot(df_counts, aes(x = species, y = n, fill = sex))
p = p + geom_col(position = "dodge")
p
options(repr.plot.width = 5, repr.plot.height = 3, repr.plot.res = 300)
p = ggplot(df_counts, aes(x = species, y = n))
p = p + geom_col()
p = p + facet_wrap(~sex)
p