- Learn how to:
- Explore data visualy
- Create layered graphs w/
ggplot2
- Readings
- R4DS: ch. 3, 7
- Data Visualization cheatsheet
ggplot2
Communicate information graphically through plots, charts, maps, etc.
R package ggplot2
implements such grammar for creating plots in R
Geometric objects convey information through their aesthetics (i.e. perceived characteristics)
Data variable(s) can be mapped to one or more aesthetics
gapminder
package provides excerpt of available datalibrary(gapminder); glimpse(gapminder) ## Observations: 1,704 ## Variables: 6 ## $ country <fct> Afghanistan, Afghanistan, Afghanistan, Afghanistan, ... ## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia... ## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992... ## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.8... ## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 1488... ## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 78...
ggplot2
ggplot( data, aes(...) ) + geom_*() + ...
gap07 = gapminder %>% filter( year == "2007") ggplot(gap07, aes(x = continent)) + geom_bar()
geom_bar
)geom_histogram
)geom_boxplot
)geom_point
)geom_line
)Variable | N/A | discrete | continuous | (time) |
---|---|---|---|---|
discrete | bar | (bar) | box | |
continuous | hist | box | points | line |
gap07 %>% group_by(continent) %>% summarise( pop = sum( as.numeric(pop) ) ) %>% ggplot(aes(x = continent, y = pop)) + geom_bar(stat = "identity")
ggplot(gap07, aes(x = lifeExp)) + geom_histogram(bins = 20)
ggplot(gap07, aes(x = continent, y = lifeExp)) + geom_boxplot()
ggplot(gap07, aes(x = gdpPercap, y = lifeExp)) + geom_point()
gapminder %>% group_by(year) %>% summarise( world_pop = sum(as.numeric(pop))) %>% ggplot(aes(x = year, y = world_pop)) + geom_line()
stat
functionggplot(gap07, aes(x = continent, y = pop) ) + stat_summary(fun.y = "sum", geom = "bar")
ggplot(gap07, aes(x = continent, y = gdpPercap) ) + geom_boxplot() + scale_y_log10()
ggplot(gap07, aes(x = gdpPercap, y = lifeExp)) + geom_point() + facet_wrap( facets = ~ continent)
gapminder %>% filter( year > 1985 ) %>% mutate( year = as.factor(year), pop = as.numeric(pop) ) %>% group_by(year, continent) %>% summarise(pop = sum(pop) ) %>% ggplot(aes(x = continent, y = pop, fill = year)) + geom_bar(stat = "identity", position = "dodge")
ggplot(gap07, aes(x = continent, y = pop) ) + stat_summary(fun.y = "sum", geom = "bar") + coord_flip()