Gapminder
library(pacman)
p_load(tidyverse, 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.
# ℹ 1,694 more rows
gapminder %>%
ggplot(aes(x = gdpPercap, y = lifeExp )) +
geom_point()
gapminder %>%
ggplot(aes(x = log(gdpPercap), y = log(lifeExp))) +
geom_point()
gapminder %>%
ggplot(aes(x = log(gdpPercap), y = log(lifeExp), color = continent, size = pop)) +
geom_point() +
geom_text(aes(label=ifelse(country=="United States",as.character(country),'')),hjust=-0.2,vjust=0)
Faceting
# 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.
# ℹ 1,694 more rows
gapminder %>%
ggplot(aes(x = log(gdpPercap), y = log(lifeExp), color = continent, size = pop)) +
geom_point() +
facet_wrap(~year)
Plot.ly faceting
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
p <- gapminder %>%
ggplot(aes(x = log(gdpPercap), y = log(lifeExp), color = continent, size = pop)) +
geom_point() +
facet_wrap(~year)
ggplotly(p)
Plot.ly annimation with ggplot.
To learn about Plot.ly R and Plot.ly ggplot.
The plot.ly book is an excellent place to start to learn about use of animation plot.ly chapter 14.
gg <- gapminder %>% ggplot(aes(log(gdpPercap), log(lifeExp), color = continent)) +
geom_point(aes(size = pop, frame = year, ids = country))
Warning in geom_point(aes(size = pop, frame = year, ids = country)): Ignoring
unknown aesthetics: frame and ids