--- title: "Week 3 Lecture 1" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) library(tidyverse) library(gapminder) ``` ### Tidy data library(tidyverse) library(ggplot2) library(gapminder) ### Wide format vs. tidy data a <- spread(gapminder %>% select(country, year, lifeExp), key=country, value = lifeExp) ### First graph in ggplot ```{r} p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y=lifeExp)) p + geom_point() ``` ```{r} p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y=lifeExp)) p + geom_point() + geom_smooth(method="loess") ``` ```{r} p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y=lifeExp)) p + geom_point() + geom_smooth(method="loess") + scale_x_continuous(labels=scales::dollar) ``` ```{r} p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y=lifeExp)) p + geom_point() + geom_smooth(method="loess") + scale_x_log10() ``` ```{r} p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y=lifeExp)) p + geom_point(color="black") + geom_smooth(method="loess", color="orange") + scale_x_log10(labels=scales::dollar) ``` ```{r} p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) p + geom_point(color = "purple") + geom_smooth(method = "loess") + scale_x_log10() ``` ```{r} p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y=lifeExp)) p + geom_point(alpha = 0.3) + geom_smooth(method = "gam", color="orange") + scale_x_log10(labels = scales::dollar) + labs(x = "GDP Per Capita", y = "Life Expectancy in Years", title = "Economic Growth and Life Expectancy", subtitle = "Data points are country-years", caption = "Source: Gapminder.") ``` ```{r} p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, color = continent, fill = continent)) p + geom_point() + geom_smooth(method = "loess") + scale_x_log10() ``` ```{r} p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) p + geom_point(mapping = aes(color = continent)) + geom_smooth(method = "lm") + scale_x_log10() ``` ```{r} p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) p + geom_point(mapping = aes(color = log(pop))) + scale_color_gradientn(colours = terrain.colors(7)) + scale_x_log10() ```