--- title: "Lab File 04" author: "Your Name Here" date: "02/08/2023" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE) ``` ## Loading Data ```{r loading} library(dplyr) library(ggplot2) life <- read.csv('https://github.com/apodkul/ppol670_01/raw/main/Data/life_expect.csv') life %>% glimpse ``` ## Reviewing geoms ```{r geoms_review} ggplot(life, aes(x = GDP_per_capita)) + geom_histogram() life %>% ggplot(aes(x = GDP_per_capita)) + geom_histogram() ggplot(life, aes(x = GDP_per_capita, y = life_expectancy)) + geom_point() ggplot(life, aes(x = GDP_per_capita, y = life_expectancy)) + geom_point(color = 'red', shape = 18) ``` ## Paired Coding(!) We're going to take five minutes to do a paired coding assignment. Find a partner. For question #1, have one user write the code (the driver) and the other review (observer). For question #2, switch roles! ```{r question1} #1: Using dplyr commands and ggplot2, display the _median_ # life expectancy by Continent in a bar chart. Try to use one piped command. ``` ```{r question2} #1: Using dplyr commands and ggplot2, create a histogram of #GDP per capita with separate panels for each Continent. #Use a non-default ggplot2 theme ``` ## Scales ```{r scales} ggplot(life, aes(x = GDP_per_capita, y = life_expectancy)) + geom_point(color = 'red', shape = 18) + scale_x_log10('GDP per Capita') ggplot(life, aes(x = GDP_per_capita, y = life_expectancy)) + geom_point(color = 'red', shape = 18) + scale_x_log10('GDP per Capita', label = scales::label_log()) ggplot(life, aes(x = GDP_per_capita, y = life_expectancy)) + geom_point(color = 'red', shape = 18) + scale_x_log10('GDP per Capita', label = scales::label_log()) ``` ## Passed Aesthetics ```{r passed_aes} ggplot(life, aes(x = GDP_per_capita, y = life_expectancy, color = Continent)) + geom_point( shape = 18) + scale_x_log10('GDP per Capita') ``` ```{r aes_diff} #What's the difference between these two? ggplot(life, aes(x = GDP_per_capita, y = life_expectancy, color = Continent)) + geom_smooth(method = 'lm') + geom_point( shape = 18) + scale_x_log10('GDP per Capita') ggplot(life, aes(x = GDP_per_capita, y = life_expectancy) ) + #geom_point(aes(color = Continent), shape = 18) + #geom_smooth(method = 'lm') + scale_x_log10('GDP per Capita') ``` ## Adapting Themes ```{r themes} #install.packages("ggthemes") ggplot(life, aes(x = GDP_per_capita, y = life_expectancy)) + geom_point(aes(color = Continent), shape = 18) + geom_smooth(method = 'lm') + scale_x_log10('GDP per Capita') + theme_minimal() + theme(legend.position = 'top') ``` ## Building Labels ```{r labels} plot_name <- ggplot(life, aes(x = GDP_per_capita, y = life_expectancy)) + geom_point(aes(color = Continent), shape = 18) + geom_smooth(method = 'lm') + scale_x_log10('GDP per Capita') + theme_minimal() + theme(legend.position = 'top') + labs(title = 'Title', subtitle = 'Subtitle', caption = 'Caption here!') plot_name ``` ## Saving Plots Locally ```{r saver} p_obj <- ggplot(life, aes(x = GDP_per_capita, y = life_expectancy)) + geom_point(aes(color = Continent), shape = 18) + geom_smooth(method = 'lm') + scale_x_log10('GDP per Capita') + theme_minimal() + theme(legend.position = 'top') + labs(title = 'Title', subtitle = 'Subtitle', caption = 'Caption here!') #ggsave(filename = 'plot_name.png', # plot = p_obj) #??ggsave for more details ``` ```{r} p_obj + ggthemes::theme_economist() ```