library(gapminder)
library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✔ ggplot2 3.3.3     ✔ purrr   0.3.3
## ✔ tibble  2.1.3     ✔ dplyr   1.0.4
## ✔ tidyr   1.0.0     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.6.2
## Warning: package 'dplyr' was built under R version 3.6.2
## ── Conflicts ───────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
gapminder_2007 <- gapminder %>% filter(year == 2007)
gapminder_2007
## # A tibble: 142 x 6
##    country     continent  year lifeExp       pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>     <int>     <dbl>
##  1 Afghanistan Asia       2007    43.8  31889923      975.
##  2 Albania     Europe     2007    76.4   3600523     5937.
##  3 Algeria     Africa     2007    72.3  33333216     6223.
##  4 Angola      Africa     2007    42.7  12420476     4797.
##  5 Argentina   Americas   2007    75.3  40301927    12779.
##  6 Australia   Oceania    2007    81.2  20434176    34435.
##  7 Austria     Europe     2007    79.8   8199783    36126.
##  8 Bahrain     Asia       2007    75.6    708573    29796.
##  9 Bangladesh  Asia       2007    64.1 150448339     1391.
## 10 Belgium     Europe     2007    79.4  10392226    33693.
## # … with 132 more rows
ggplot(data = gapminder) +
  geom_point(mapping = aes(x= gdpPercap,
                y = lifeExp))

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

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

ggplot(data = gapminder_2007,
  mapping = aes(x = gdpPercap,
                y = lifeExp, 
                color=continent,
                size=pop)) + 
  geom_point() + 
  scale_x_log10()

ggplot(data = gapminder_2007,
  mapping = aes(x = gdpPercap,
                y = lifeExp, 
                color=continent,
                size=pop)) + 
  geom_point() + 
  scale_x_log10() + 
  scale_color_viridis_d()

ggplot(data = gapminder_2007,
  mapping = aes(x = gdpPercap,
                y = lifeExp, 
                color=continent,
                size=pop)) + 
  geom_point() + 
  scale_x_log10() + 
  facet_wrap(vars(continent))

ggplot(data = gapminder_2007,
  mapping = aes(x = gdpPercap,
                y = lifeExp, 
                color=continent,
                size=pop)) + 
  geom_point() + 
  scale_x_log10() +
  labs(title = "Health and income are positively correlated", 
       subtitle = "2007 data", 
       x = "Income (GDP/capita",
       y = "Health (life expectancy)",
       color = "Continent",
       size = "Population",
       caption = "Source: The Gapminder Project")

ggplot(data = gapminder_2007,
  mapping = aes(x = gdpPercap,
                y = lifeExp, 
                color=continent,
                size=pop)) + 
  geom_point() + 
  scale_x_log10() +
  theme_dark()