class: center, middle, title-slide # Data visualisation - solutions ### Olivier Gimenez ### last updated: 2022-05-22 --- ## Question 1a: histogram of body mass .small-font[ ```r penguins %>% ggplot() + aes(x = body_mass_g) + geom_histogram() ``` ] .center[ <!-- --> ] --- ## Question 1b: a color per species .small-font[ ```r penguins %>% ggplot() + aes(x = body_mass_g) + geom_histogram(aes(fill = species)) ``` ] .center[ <!-- --> ] --- ## Question 1c: darkorange, purple and cyan4 .small-font[ ```r penguins %>% ggplot() + aes(x = body_mass_g) + geom_histogram(aes(fill = species)) + scale_fill_manual(values = c("darkorange","purple","cyan4")) ``` ] .center[ <!-- --> ] --- ## Question 1d: adjust transparency .small-font[ ```r penguins %>% ggplot() + aes(x = body_mass_g) + geom_histogram(aes(fill = species), alpha = 0.5) + scale_fill_manual(values = c("darkorange","purple","cyan4")) ``` ] .center[ <!-- --> ] --- ## Question 1e: add titles .pull-left.tiny-font[ ```r penguins %>% ggplot() + aes(x = body_mass_g) + geom_histogram(aes(fill = species), alpha = 0.5) + scale_fill_manual(values = c("darkorange", "purple", "cyan4")) + labs(x = "Body mass (g)", y = "Frequency", title = "Penguin body mass", fill = "Penguin species") ``` ] .pull-right[ <!-- --> ] --- ## Question 1f: change theme .pull-left.tiny-font[ ```r penguins %>% ggplot() + aes(x = body_mass_g) + geom_histogram(aes(fill = species), alpha = 0.5) + scale_fill_manual(values = c("darkorange", "purple", "cyan4")) + labs(x = "Body mass (g)", y = "Frequency", title = "Penguin body mass", fill = "Penguin species") + theme_minimal() ``` ] .pull-right[ <!-- --> ] --- ## Question 1g: split by sex .pull-left.tiny-font[ ```r penguins %>% # filter out penguins w/ missing sex filter(!is.na(sex)) %>% ggplot() + aes(x = body_mass_g) + geom_histogram(aes(fill = species), # fill histogram, adjust transparency alpha = 0.5) + # change colour scale_fill_manual(values = c("darkorange", "purple", "cyan4")) + labs(x = "Body mass (g)", # x lab y = "Frequency", # y lab title = "Penguin body mass", fill = "Penguin species") + theme_minimal() + # change theme # hist by sex, w/ diff X scale facet_wrap(~sex, scales = "free_x") ``` ] .pull-right[ <!-- --> ] --- ## Question 2a: scatter plot .tiny-font[ ```r penguins %>% ggplot() + aes(x = flipper_length_mm, y = body_mass_g) + geom_point() + theme_minimal() ``` ] .center[ <!-- --> ] --- ## Question 2b: species-specific shapes .pull-left.tiny-font[ ```r penguins %>% ggplot() + aes(x = flipper_length_mm, y = body_mass_g) + geom_point(aes(shape = species), size = 3, alpha = 0.8) + theme_minimal() ``` ] .pull-right[ <!-- --> ] --- ## Question 2c: species-specific colors .pull-left.tiny-font[ ```r penguins %>% ggplot() + aes(x = flipper_length_mm, y = body_mass_g) + geom_point(aes(color = species), size = 3, alpha = 0.8) + theme_minimal() ``` ] .pull-right[ <!-- --> ] --- ## Question 2d: species-specific shapes and colors .pull-left.tiny-font[ ```r penguins %>% ggplot() + aes(x = flipper_length_mm, y = body_mass_g) + geom_point(aes(color = species, shape = species), size = 3, alpha = 0.8) + theme_minimal() ``` ] .pull-right[ <!-- --> ] --- ## Question 2e: add titles .tiny-font[ ```r penguins %>% ggplot() + aes(x = flipper_length_mm, y = body_mass_g) + geom_point(aes(color = species, shape = species), size = 3, alpha = 0.8) + scale_color_manual(values = c("darkorange","purple","cyan4")) + labs(title = "Penguin body mass wrt flipper length", subtitle = "for Adelie, Chinstrap and Gentoo species", x = "Flipper length (mm)", y = "Body mass (g)", color = "Penguin species", shape = "Penguin species") + theme_minimal() ``` ] --- ## Question 2e .center[ <!-- --> ]