We need to install and then load the following packages
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.7 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(gapminder)
library(datasets)
hist(gapminder$lifeExp, main = "Life Expectancy", xlab = "years")
plot(lifeExp ~ gdpPercap, gapminder, main = "Life Expectancy vs. GDP",
xlab = "GDP per capita", ylab = "Life Expectancy")
Instead of specifying the second argument, we could plot the vectors directly:
plot(gapminder$lifeExp ~ gapminder$gdpPercap) # no second argument
The above plot is clearer on the log-scale. This is because countries tend to be exponentially wealthier than each other in terms of GDP per capita. Note for instance that the x-axis in the above plot contains large values.
plot(lifeExp ~ log(gdpPercap), gapminder,
main = "Life Expectancy and GDP on log scale")
The relationship is much clearer after transforming one of the variables!