Byeong-Hak Choe
February 10, 2022
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4 ## ✓ tibble 3.1.6 ✓ dplyr 1.0.8 ## ✓ tidyr 1.1.4 ✓ stringr 1.4.0 ## ✓ readr 2.1.2 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ── ## x dplyr::filter() masks stats::filter() ## x dplyr::lag() masks stats::lag()
library(skimr) # a better summary of data frame library(scales) # scales for ggplot
## ## Attaching package: 'scales'
## The following object is masked from 'package:purrr': ## ## discard
## The following object is masked from 'package:readr': ## ## col_factor
theme_set(theme_minimal()) # setting the minimal theme for ggplot
Use theme()
if you want to tweak the display of an existing theme.
The following lists the ggplot themes:
theme | theme |
---|---|
theme_grey() | theme_gray() |
theme_bw() | theme_linedraw() |
theme_light() | theme_dark() |
theme_minimal() | theme_classic() |
theme_void() | theme_test() |
gapminder
package include the gapminder
data frame.library(gapminder) gapminder
## # A tibble: 1,704 × 6 ## country continent year lifeExp pop gdpPercap ## <fct> <fct> <int> <dbl> <int> <dbl> ## 1 Afghanistan Asia 1952 28.8 8425333 779. ## 2 Afghanistan Asia 1957 30.3 9240934 821. ## 3 Afghanistan Asia 1962 32.0 10267083 853. ## 4 Afghanistan Asia 1967 34.0 11537966 836. ## 5 Afghanistan Asia 1972 36.1 13079460 740. ## 6 Afghanistan Asia 1977 38.4 14880372 786. ## 7 Afghanistan Asia 1982 39.9 12881816 978. ## 8 Afghanistan Asia 1987 40.8 13867957 852. ## 9 Afghanistan Asia 1992 41.7 16317921 649. ## 10 Afghanistan Asia 1997 41.8 22227415 635. ## # … with 1,694 more rows
skim(gapminder)
p <- ggplot(data = gapminder) p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) p
p + geom_point()
p + geom_smooth()
p + geom_point() + geom_smooth()
p + geom_point() + geom_smooth(method = "lm")
p + geom_point() + geom_smooth(method = "gam") + scale_x_log10()
p + geom_point() + geom_smooth(method = "gam") + scale_x_log10(labels = scales::dollar)
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, color = continent)) p
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, color = "purple")) p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) p + geom_point(color = "purple") + geom_smooth(method = "loess") + scale_x_log10()
p + geom_point(alpha = 0.3) + geom_smooth(color = "orange", se = FALSE, size = 8, method = "lm") + scale_x_log10()
p + geom_point(alpha = 0.3) + geom_smooth(method = "gam") + 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.")
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, color = continent)) p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, color = continent, fill = continent)) p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) p + geom_point(mapping = aes(color = continent)) + geom_smooth(method = "loess") + scale_x_log10()
p + geom_point(mapping = aes(color = log(pop))) + scale_x_continuous(trans = scales::log_trans()) # natural log scale
knitr::opts_chunk$set(fig.width=8, fig.height=5)
p + geom_point()
ggsave(filename = "my_figure.png")
ggsave(filename = "my_figure.pdf")
p_out <- p + geom_point() + geom_smooth(method = "loess") + scale_x_log10() ggsave("my_figure.pdf", plot = p_out)
library(here) ggsave(here("figures", "lifexp_vs_gdp_gradient.pdf"), plot = p_out)
here()
starts at the directory where the project is created.
Create the “figures” folder in the directory where the project is created.
ggsave("lifexp_vs_gdp_gradient.pdf", plot = p_out, height = 8, width = 10, units = "in")
p <- ggplot(data = gapminder, mapping = aes(x = year, y = gdpPercap)) p + geom_line()
p + geom_line(aes(group=country))
p + geom_line(aes(group = country)) + facet_wrap(~ continent)
p + geom_line(color="gray70", aes(group = country)) + geom_smooth(size = 1.1, method = "loess", se = FALSE) + scale_y_log10(labels=scales::dollar) + facet_wrap(~ continent, ncol = 5) + labs(x = "Year", y = "GDP per capita", title = "GDP per capita on Five Continents")
p + geom_line(color="gray70", aes(group = country)) + geom_smooth(size = 1.1, method = "loess", se = FALSE) + scale_y_log10(labels=scales::dollar) + facet_wrap(~ continent, ncol = 5) + labs(x = "Year", y = "GDP per capita", title = "GDP per capita on Five Continents") + theme(axis.text.x = element_text(angle = 45), axis.title.x = element_text(margin = margin(t = 25)))