--- title: "Live coding 3" author: "Andrew Irwin" date: "2021-01-21 08:35 Atlantic" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) library(tidyverse) library(gapminder) ``` # Agenda * Questions arising from lessons, tasks * Task 4 update: some have not accepted invitation to join "Dalhousie-AndrewIrwin-Teaching" organization on GitHub. Look for email, or a link on the github web page, or write me for help. * Use ggplot tools three different ways The plan for the live coding is for us to work together to make some plots. I'll start by recreating some plots from the book or lessons. Then we will review the tools we've learned to use to create new plots. Finally we'll try to create new plots from our imagination. ## Replicate some plots from memory Use the gapminder data to draw a scatterplot of life expectancy as a function of GDP per capita. ```{r} gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp, color = continent)) + geom_point() ``` Revise the plot to colour the points by continent. Revise again to make all the points blue, regardless of country or continent. ```{r} gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp)) + geom_point(olour = "blue") ``` ## Combine the tools we've learned Start by reviewing the aesthetic mappings (plus I've added a few): * ggplot(aes( ... )) where we use one or more of * x = * y = * color = * fill = * shape = * size = * label = * group = * alpha = * linetype = * stroke = Aesthetics are listed on the help pages for each geom. Not all aesthetics make sense for all geometries! How many geoms can you remember and use? * geom_point, geom_line, geom_histogram, geom_boxplot, geom_bar, geom_text, geom_smooth, geom_jitter Here are some functions we only used a bit: * stat_summary * scale_x_log10 Changing the appearance of a plot * xlim, ylim * labs(x="", y="", title="", subtitle="", caption="", fill="", color="") * theme_bw * theme(text = element_text(size = 20)) Modify the plot we created above to use lines instead of points. Plot GDP on a log scale. ```{r} gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp, group = country, colour = continent)) + geom_line() + scale_x_log10() ``` Pick a few geoms and other features listed above to make a few new plots. ```{r} gapminder %>% ggplot(aes(x = lifeExp, y = continent)) + geom_boxplot() ``` ```{r} gapminder %>% ggplot(aes(x = lifeExp, y = continent)) + geom_jitter() + geom_boxplot() gapminder %>% ggplot(aes(x = lifeExp, y = continent)) + geom_boxplot() + geom_jitter(height = 0.2, color = "gray50", alpha = 0.5) ``` ```{r} gapminder %>% ggplot(aes(x = lifeExp, y = continent)) + stat_summary() + xlim(27, 85) ``` Example from `?geom_line` ```{r} ggplot(economics, aes(date, unemploy)) + geom_line(size = 1.3, linetype = 5) ``` ## Create new plots In this section we will try to draw a new plot. First, think of something you'd like to show. Then use the language of grammar of graphics to describe what you want. Then write the R code to do it. ### Histogram Show how body mass of penguins varies within and across species. ```{r warning=FALSE} library(palmerpenguins) penguins %>% ggplot(aes(x = body_mass_g, fill = species)) + geom_histogram(binwidth = 225) ``` ### Boxplot Distribution of life expectancy for each continent, coloured by year. ```{r} gapminder %>% ggplot(aes(x = lifeExp, fill = factor(year))) + geom_histogram() ``` ### Averages and uncertainties Average life expectancy for each year with data, broken down by continent. ```{r} gapminder %>% ggplot(aes(y = lifeExp, x = year, color = continent)) + stat_summary() ``` ### Smooths Life expectancy over year, grouped by continent, and smoothed. ```{r} gapminder %>% ggplot(aes(x = year, y = lifeExp, color = continent)) + geom_smooth() + geom_jitter(width = 0.2) ``` ## Thinking about visualizations Compare this scatterplot with `geom_jitter` and `geom_point`. What is the difference? Which is better? Why? Is one misleading? ```{r} mpg %>% ggplot(aes(x = cty, y = hwy)) + geom_jitter() ```