Share
Embed
Eight visualisation challenges with ggplot2
Presented to the NYC datavis meetup.
Hadley Wickham
November 17, 2016
Transcript
67 slides
-
Hadley Wickham @hadleywickham Chief Scientist, RStudio Solving 8 visualisation
challenges with ggplot2 November 2016 -
• • • • • • • • • •
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 20 30 40 2 3 4 5 6 7 displ hwy class • • • • • • • 2seater compact midsize minivan pickup subcompact suv Two seaters (sports cars) are an exception because of their light weight Fuel efficiency generally decreases with engine size Data from fueleconomy.gov -
ggplot(mpg, aes(displ, hwy)) + geom_point(aes(color = class)) + geom_smooth(se =
FALSE, method = "loess") + labs( title = "Fuel efficiency generally ...", subtitle = "Two seaters (sports cars) ...", caption = "Data from fueleconomy.gov" ) Accessed with the labs() function -
Stages of visualisation system popularity 1. Someone used it and
complained about a bug 2. Someone used it in an academic paper 3. Someone used it in a newspaper 4.Someone used it to commit academic fraud 5. So many people use it that google has autocompletes for bad graphics ideas -
Isenberg, Petra, et al. "A study on dual-scale data charts."
IEEE Transactions on Visualization and Computer Graphics 17.12 (2011): 2469-2478. https://www.lri.fr/~isenberg/publications/papers/Isenberg_2011_ASO.pdf -
• • • • • • • • • •
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 20 30 40 6 8 10 12 14 16 18 20 2 3 4 5 6 7 displ mpg l / 100 km -
ggplot(mpg, aes(displ, hwy)) + geom_point() + scale_y_continuous( "mpg", sec.axis =
sec_axis( ~ 235 / ., name = "l / 100 km", breaks = seq(2, 20, by = 2) ) ) Only 1-to-1 transformations are allowed function(x) { 235 / x } -
• • • • • • • • • •
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 20 30 40 6 8 10 12 14 16 18 20 2 3 4 5 6 7 displ mpg l / 100 km -
• • • • • • • • • •
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • corvette caravan 2wd altima forester awd toyota tacoma 4wd jetta new beetle 20 30 40 2 3 4 5 6 7 displ hwy class • • • • • • • 2seater compact midsize minivan pickup subcompact suv geom_text() -
• • • • • • • • • •
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • corvette caravan 2wd altima forester awd toyota tacoma 4wd jetta new beetle 20 30 40 2 3 4 5 6 7 displ hwy class • • • • • • • 2seater compact midsize minivan pickup subcompact suv geom_label() -
• • • • • • • • • •
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • corvette caravan 2wd altima forester awd toyota tacoma 4wd jetta new beetle 20 30 40 2 3 4 5 6 7 displ hwy class • • • • • • • 2seater compact midsize minivan pickup subcompact suv https://github.com/slowkow/ggrepel geom_label_repel() -
• • • • • • • • • •
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • corvette caravan 2wd altima forester awd toyota tacoma 4wd jetta new beetle 20 30 40 2 3 4 5 6 7 displ hwy class • • • • • • • 2seater compact midsize minivan pickup subcompact suv dev version -
Two difference between a factor and a string: 1.Fixed set
of possible values 2.Arbitrary order -
relig <- gss_cat %>% group_by(relig) %>% summarise( tvhours = mean(tvhours,
na.rm = TRUE), n = n() ) Some data from the general social survey -
• • • • • • • • • •
• • • • • No answer Don't know Inter−nondenominational Native american Christian Orthodox−christian Moslem/islam Other eastern Hinduism Buddhism Other None Jewish Catholic Protestant 2 3 4 tvhours relig -
• • • • • • • • • •
• • • • • Other eastern Hinduism Buddhism Orthodox−christian Moslem/islam Jewish None No answer Other Christian Inter−nondenominational Catholic Protestant Native american Don't know 2 3 4 tvhours fct_reorder(relig, tvhours) -
by_age <- gss_cat %>% filter(!is.na(age)) %>% group_by(age, marital) %>% count()
%>% mutate(prop = n / sum(n)) You have the same problem with more dimensions -
0.00 0.25 0.50 0.75 1.00 20 40 60 80 age
prop marital No answer Never married Separated Divorced Widowed Married -
0.00 0.25 0.50 0.75 1.00 20 40 60 80 age
prop marital Widowed Married Divorced Never married No answer Separated -
An explicit missing value (NA) is the presence of an
absence; an implicit missing value is the absence of a presence. -
df <- tibble(x = 1:4) df %>% ggplot(aes(x)) + geom_histogram(binwidth
= 1) Equivalent ggplot2 code is a little longer -
0.00 0.25 0.50 0.75 1.00 1 2 3 4 x
count (0.5, 1.5] (1.5, 2.5] (2.5, 3.5] (3.5, 4.5] Thanks to Randall Pruim -
df %>% ggplot(aes(x)) + geom_histogram( binwidth = 1, boundary =
0 ) df %>% ggplot(aes(x)) + geom_histogram( binwidth = 1, boundary = 0, closed = "left" ) -
0.0 0.5 1.0 1.5 2.0 1 2 3 4 x
count [1, 2] (2, 3] (3, 4] -
0.0 0.5 1.0 1.5 2.0 1 2 3 4 x
count (0.5, 1.5] [1, 2) [2, 3) [3, 4] -
0.0 0.5 1.0 1.5 2.0 1 2 3 4 x
count (0.5, 1.5] [0.99999, 1.99999) [1.99999, 2.99999) [2.99999, 4.00001] -
0 20 40 60 2seater compact midsize minivan pickup subcompact
suv class count ggplot(mpg, aes(class)) + geom_bar(colour = "white") -
0 20 40 60 2seater compact midsize minivan pickup subcompact
suv class count ggplot(mpg, aes(class, group = id)) + geom_bar(col = "white") -
0 20 40 60 2seater compact midsize minivan pickup subcompact
suv class count drv 4 f r ggplot(mpg, aes(class, group = id, fill = drv)) + geom_bar(col = "white") -
0 20 40 60 2seater compact midsize minivan pickup subcompact
suv class count drv 4 f r ggplot(mpg, aes(class, fill = drv)) + geom_bar(col = "white") -
class_mpg <- mpg %>% group_by(class) %>% summarise( mean = mean(hwy),
se = 1.96 * sd(hwy) / sqrt(n()) ) Another type of bar chart displays summaries -
0 10 20 2seater compact midsize minivan pickup subcompact suv
class mean ggplot(class_mpg, aes(class, mean)) + geom_bar(stat = "identity") -
0 10 20 2seater compact midsize minivan pickup subcompact suv
class mean ggplot(class_mpg, aes(class, mean)) + geom_col() # Thanks to Bob Rudis -
• • • • • • • 20 24 28
2seater compact midsize minivan pickup subcompact suv class mean -
• • • • • • • 15 20 25
30 2seater compact midsize minivan pickup subcompact suv class mean -
Many of the features I discussed here have been added
in recent versions of ggplot2. See the release notes for more detail.