http://www.spiegel.de/wissenschaft/natur/bild-1119424-1067142.html
http://www.spiegel.de/wissenschaft/natur/bild-1119539-1067290.html
http://www.spiegel.de/wissenschaft/natur/bild-1119424-1067142.html
http://www.spiegel.de/wissenschaft/natur/bild-1119539-1067290.html
Today's goal: building a final plot
Axis transformation
Labeling
Annotation & other geometric shapes
Animation & Interactivity
Saving
library(gapminder); library(dplyr); library(ggplot2) gapminder %>% head(3)
## # A tibble: 3 × 6 ## country continent year lifeExp pop gdpPercap ## <fctr> <fctr> <int> <dbl> <int> <dbl> ## 1 Afghanistan Asia 1952 28.801 8425333 779.4453 ## 2 Afghanistan Asia 1957 30.332 9240934 820.8530 ## 3 Afghanistan Asia 1962 31.997 10267083 853.1007
Ultimate goal: How does life expectency vary based on gdp per capita?
p <- ggplot(gapminder, aes(gdpPercap, lifeExp)) + geom_point() p
p <- ggplot(gapminder, aes(gdpPercap, lifeExp,
size = pop, color = continent)) +
geom_point()
p
p <- ggplot(gapminder, aes(gdpPercap, lifeExp,
size = pop, color = continent)) +
geom_point() +
scale_x_log10()
p
gapminder1 <- gapminder %>% mutate(gdp.log10 = log(gdpPercap, 10)) set.seed(5) gapminder1 %>% arrange(gdpPercap) %>% sample_n(2)
## # A tibble: 2 × 7 ## country continent year lifeExp pop gdpPercap gdp.log10 ## <fctr> <fctr> <int> <dbl> <int> <dbl> <dbl> ## 1 India Asia 1987 58.553 788000000 976.5127 2.989678 ## 2 Jamaica Americas 1997 72.262 2531311 7121.9247 3.852597
gapminder2 <- gapminder1 %>%
filter(year == 2007, continent == "Asia")
p <- ggplot(gapminder2, aes(gdpPercap, lifeExp)) +
geom_point(aes(size = pop)) +
scale_x_log10()
p
p + geom_text(aes(label = country))
library(ggrepel) p + geom_text_repel(aes(label = country))
p + geom_label_repel(aes(label = country))
p1 <- p + geom_label_repel(data = filter(gapminder2,
lifeExp < 50 | lifeExp >80| pop > 10^9),
aes(label = country),
box.padding = unit(0.35, "lines"),
point.padding = unit(0.5, "lines"),
segment.color = 'red')
p1
p1 + geom_text(aes(x = 2000, y = 50), label = "Low GDP, life exp.",
colour = "red") +
geom_text(aes(x = 20000, y = 70), label = "High GDP, life exp.",
colour = "red")
Steps to animation (Mac.. PC?)
sudo port install ImageMagickgganimate packagelibrary(gganimate)
library(gapminder)
library(ggplot2)
theme_set(theme_bw())
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop,
color = continent, frame = year)) +
geom_point() +
scale_x_log10()
#gg_animate(p)
library(plotly) dsamp <- sample_n(diamonds, 1000) ggplot(dsamp, aes(carat, price, colour=clarity)) + geom_point()
#ggsave(p1, file = "gapminder.pdf", width = 6, height = 5) #ggsave(p1, file = "gapminder.png", width = 6, height = 5)