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[ ![](2_datavisualisation_solution_files/figure-html/question1a-1.svg)<!-- --> ] --- ## Question 1b: a color per species .small-font[ ```r penguins %>% ggplot() + aes(x = body_mass_g) + geom_histogram(aes(fill = species)) ``` ] .center[ ![](2_datavisualisation_solution_files/figure-html/question1b-1.svg)<!-- --> ] --- ## 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[ ![](2_datavisualisation_solution_files/figure-html/question1c-1.svg)<!-- --> ] --- ## 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[ ![](2_datavisualisation_solution_files/figure-html/question1d-1.svg)<!-- --> ] --- ## 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[ ![](2_datavisualisation_solution_files/figure-html/question1e-1.svg)<!-- --> ] --- ## 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[ ![](2_datavisualisation_solution_files/figure-html/question1f-1.svg)<!-- --> ] --- ## 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[ ![](2_datavisualisation_solution_files/figure-html/question1g-1.svg)<!-- --> ] --- ## Question 2a: scatter plot .tiny-font[ ```r penguins %>% ggplot() + aes(x = flipper_length_mm, y = body_mass_g) + geom_point() + theme_minimal() ``` ] .center[ ![](2_datavisualisation_solution_files/figure-html/question2a-1.svg)<!-- --> ] --- ## 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[ ![](2_datavisualisation_solution_files/figure-html/question2b-1.svg)<!-- --> ] --- ## 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[ ![](2_datavisualisation_solution_files/figure-html/question2c-1.svg)<!-- --> ] --- ## 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[ ![](2_datavisualisation_solution_files/figure-html/question2d-1.svg)<!-- --> ] --- ## 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[ ![](2_datavisualisation_solution_files/figure-html/question2e-1.svg)<!-- --> ]