2. Use built-in gapminder dataset
data(gapminder)
head(gapminder)
## # A tibble: 6 × 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.
tail(gapminder)
## # A tibble: 6 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Zimbabwe Africa 1982 60.4 7636524 789.
## 2 Zimbabwe Africa 1987 62.4 9216418 706.
## 3 Zimbabwe Africa 1992 60.4 10704340 693.
## 4 Zimbabwe Africa 1997 46.8 11404948 792.
## 5 Zimbabwe Africa 2002 40.0 11926563 672.
## 6 Zimbabwe Africa 2007 43.5 12311143 470.
dim(gapminder)
## [1] 1704 6
str(gapminder)
## tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame)
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ year : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ lifeExp : num [1:1704] 28.8 30.3 32 34 36.1 ...
## $ pop : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
## $ gdpPercap: num [1:1704] 779 821 853 836 740 ...
3. Visualization (step by step)
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp))

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point()

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point() +
geom_line()

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point() +
geom_smooth()
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp, col = continent)) +
geom_point()

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
geom_smooth(method="loess")
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_smooth(method="loess") +
scale_x_log10()
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent)) +
geom_smooth(method = "loess") +
scale_x_log10() +
xlab("Log GDP per Capita") +
ylab("Life Expectancy") +
ggtitle("Association between GDP Per Capita and Life Expectancy")
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent)) +
geom_smooth(method = "loess") +
scale_x_log10() +
xlab("Log GDP per Capita") +
ylab("Life Expectancy") +
ggtitle("Association between GDP Per Capita and Life Expectancy") +
theme(plot.title = element_text(lineheight = 0.8, face = "bold", hjust = 0.5))
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent)) +
geom_smooth(method = "loess") +
scale_x_log10() +
xlab("Log GDP per Capita") +
ylab("Life Expectancy") +
ggtitle("Association between GDP Per Capita and Life Expectancy") +
theme(plot.title = element_text(lineheight = 0.8, face = "bold", hjust = 0.5)) +
theme(axis.title.x = element_text(color = "blue", size = 10, face = "bold"),
axis.text.x = element_text(angle = 45, vjust = 0.5, size = 10, face = "bold"),
axis.title.y = element_text(color = "blue", size = 14, face = "bold"),
axis.text.y = element_text(angle = 90, vjust = 0.5, size = 10, face = "bold"))
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent)) +
geom_smooth(method = "loess") +
scale_x_log10() +
xlab("Log GDP per Capita") +
ylab("Life Expectancy") +
ggtitle("Association between GDP Per Capita and Life Expectancy") +
scale_x_continuous(breaks = seq(0, 90000, 30000)) +
scale_y_continuous(breaks = seq(0, 100, 20)) +
theme_economist()
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color=continent)) +
geom_smooth(method="loess") +
scale_x_log10() +
xlab("Log GDP per Capita") +
ylab("Life Expectancy") +
ggtitle("Association between GDP Per Capita and Life Expectancy") +
theme_economist()
## `geom_smooth()` using formula = 'y ~ x'
