1. Import library

suppressMessages(library(plotly))
suppressMessages(library(tidyverse))
suppressMessages(library(gapminder))
suppressMessages(library(ggplot2))
suppressMessages(library(ggthemes))
suppressMessages(library(gganimate))
suppressMessages(library(gganimate))
suppressMessages(library(gifski))

2. Import data

data(gapminder)

3. Visualization

3.1 Life Expectancy for each contry

ggplot(gapminder, aes(x = year, y = lifeExp, col = continent)) + geom_point() +
  stat_smooth(method = "lm", se = FALSE) +
  facet_wrap(~country)
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data=gapminder, aes(x=year, y=lifeExp, group=country, col=continent)) + 
  geom_line() + 
  facet_grid(. ~continent) + 
  stat_smooth(aes(group = 1))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

data2007 = gapminder %>% 
  filter(year == 2007) %>% 
  mutate(size = sqrt(pop), pop_m = round((pop / 1e6), 1))

3.2 Scatterplot

# Plot scatterplot of most recent year 
ggplot(data2007, aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

# add aesthetic of size by population
ggplot(data2007, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() + 
  aes(size=pop_m)

# add aesthetic of color by continent
ggplot(data2007, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() + 
  aes(size=pop_m) + 
  aes(color=continent)

# add title, update axes labels
ggplot(data2007, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() + 
  aes(size=pop_m) + 
  aes(color=continent) + 
  ggtitle('Health & Wealth of Nations for 2007') +
  xlab('GDP per capita ($/year)') +
  ylab('Life expectancy (years)') +
  theme_economist()

# label legend
ggplot(data2007, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() + 
  aes(size=pop_m) + 
  aes(color=continent) + 
  ggtitle('Health & Wealth of Nations for 2007') +
  xlab('GDP per capita ($/year)') +
  ylab('Life expectancy (years)') +
  scale_colour_discrete(name='Continent') +
  scale_size_continuous(name='Population (M)')

3.3 Boxplot

# boxplot by continent
b = ggplot(gapminder, aes(x = continent, y = lifeExp)) +
  geom_boxplot()

b

# match color to continents, like scatterplot
b = b +
  aes(fill = continent)
b

# drop legend, add title, update axes labels
b = b +
  theme(legend.position='none') +
  ggtitle('Life Expectancy by Continent for 2007') +
  xlab('Continent') +
  ylab('Life expectancy (years)')
b

3.5 Interactive: plotly

ggplot2 | plotly

# scatterplot (Note: key=country shows up on rollover)
s = ggplot(gapminder, aes(x=gdpPercap, y=lifeExp, key=country)) +
  geom_point()

ggplotly(s)
# boxplot
ggplotly(b)
plot_ly(data2007, x = ~gdpPercap, y = ~lifeExp, type = 'scatter', 
        mode = 'markers', 
        marker = list(symbol = 'circle', sizemode = 'diameter',
                      line = list(width = 2, color = '#FFFFFF')),
        size = ~size, color = ~continent,
        text = ~paste0('Country:', country, '<br>Life Expectancy:', lifeExp, '<br>GDP:', gdpPercap,
                       '<br>Pop.:', pop)) %>% 
  layout(title = 'Life Expectancy v. Per Capita GDP, 2007',
         xaxis = list(title = 'GDP per capital (2000 dolars)',
                      gridColor = 'white',
                      zerolinewidth = 1,
                      ticklen = 5,
                      gridwidth = 2),
         yaxis = list(title = 'Life Expectancy (years)',
                      gridColor = 'white',
                      zerolinewidth = 1,
                      ticklen = 5,
                      gridwidth = 2),
         paper_bgcolor = 'rbg(243,243,243)',
         plot_bgcolor = 'rbg(243,243,243)')
## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

3.6 Dynamic graph

p <- gapminder %>% 
  mutate(pop_m = pop / 1e6) %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, size = pop_m, color = continent, frame = year)) +
  labs(x="GDP per Capital", y = "Life expectancy at birth (years)", 
       color = 'Continent',size = "Population (millions)") + 
  ylim(30,100) + 
  geom_point() 

ggplotly(p)
p1 <- gapminder %>% mutate(pop_m = pop / 1e6) %>%
 ggplot(aes(x = gdpPercap, y = lifeExp, size = pop_m, color = continent, frame = year)) +
  labs(x="GDP per capital", y = "Life expectancy at birth (years)", 
       color = 'Continent',size = "Population (millions)") + 
  ylim(30,100) +
  geom_point() +
  #ggtitle("Year: {frame_time}") +
  transition_time(year) +
  ease_aes("linear") +
  enter_fade() +
  exit_fade()

animate(p1,fps = 4, width = 600, height = 400, renderer = gifski_renderer())
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).