---
title: "Lab File 04"
author: "Your Name Here"
date: "02/04/2026"
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/ppol6803_03/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.
life %>% 
  ggplot(aes(x = Continent, y = life_expectancy)) + 
  geom_bar(stat = 'summary', fun = median)
  

life %>% 
  group_by(Continent) %>% 
  summarize(Median = median(life_expectancy)) %>%
  ggplot(aes(x = Continent, y = Median))  +
  geom_bar(stat = 'identity')


```

```{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 
life %>% 
  ggplot(aes(x = GDP_per_capita)) + 
  geom_histogram() + 
  facet_wrap(~Continent) + 
  theme_linedraw()

```

## 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())


```

## 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', color = 'pink', se = F) +
  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_stata()
```



