6. Components of the grammar
Data Variables of interest
Aesthetics
x-axis
y-axis
colour
fill
size
labels
alpha
shape
line width
line type
Geometries point line histogram bar boxplot
Facets columns rows
Statistics binning smoothing descriptive inferential
Coordinates cartesian fixed polar limits
Themes Not data, but important for overall impact
Adapted from DataCamp
8. Relationship between table (width of top of
diamond relative to widest point) and price?
diamonds %>% ggplot(aes(x = table, y = price))
+ geom_point()
9. Relationship between carat (weight) and price?
diamonds %>% ggplot(aes(x = carat, y = price))
+ geom_point()
10. Relationship between carat (weight) and price?
[How many diamonds?]
diamonds %>% ggplot(aes(x = carat, y = price))
+ geom_point(alpha = 0.1)
11. Relationship between carat (weight) and price?
[Lots of small diamonds!]
diamonds %>% ggplot(aes(x = carat, y = price)) +
geom_hex()
12. Components of the grammar
Data Variables of interest
Aesthetics
x-axis
y-axis
colour
fill
size
labels
alpha
shape
line width
line type
Geometries point line histogram bar boxplot
Facets columns rows
Statistics binning smoothing descriptive inferential
Coordinates cartesian fixed polar limits
Themes Not data, but important for overall impact
Adapted from DataCamp
13. Relationship between clarity and price?
Worse ——————————————————> Better
diamonds %>% ggplot(aes(x = clarity, y = price)) +
geom_boxplot()
14. How many diamonds with each clarity?
Worse ——————————————————> Better
diamonds %>% ggplot(aes(x = clarity, fill = clarity)) +
geom_bar()
15. Relationship between cut and price?
Worse ——————————————————> Better
diamonds %>% ggplot(aes(x = cut, y = price)) +
geom_boxplot()
16. Relationship between cut and price?
Worse ——————————————————> Better
diamonds %>% ggplot(aes(x = cut, y = price)) +
geom_boxplot()
17. Relationship between cut and price?
Worse ——————————————————> Better
diamonds %>% ggplot(aes(x = cut, y = price)) +
geom_violin()
18. Components of the grammar
Data Variables of interest
Aesthetics
x-axis
y-axis
colour
fill
size
labels
alpha
shape
line width
line type
Geometries point line histogram bar boxplot
Facets columns rows
Statistics binning smoothing descriptive inferential
Coordinates cartesian fixed polar limits
Themes Not data, but important for overall impact
Adapted from DataCamp
19. Relationship between price and carat grouped by cut and clarity?
diamonds %>% ggplot(aes(x = carat, y = price)) + geom_point() + facet_grid(cut~clarity)
20. Relationship between price and carat grouped by cut and clarity (with trend)?
diamonds %>% ggplot(aes(x = carat, y = price)) + geom_point() + facet_grid(cut~clarity) + geom_smooth(method =
"lm")
21. Relationship between table parameter (width of top of diamond relative to
widest point, converted on the fly into a factor) and price, faceted by cut?
diamonds %>%
mutate(TableIsSmall = factor(table < median(table)))%>%
ggplot(aes(x = carat, y = price, colour = TableIsSmall)) + geom_point() + facet_grid(.~cut)
22. Relationship between table parameter (width of top of diamond relative to
widest point, converted on the fly into a factor) and price, faceted by cut?
diamonds %>%
mutate(TableIsSmall = factor(table < median(table)))%>%
ggplot(aes(x = carat, y = price, colour = TableIsSmall)) + geom_point() + facet_grid(.~cut)
Most diamonds with a small table (TableIsBig == FALSE) have an “Ideal” cut…
23. Components of the grammar
Data Variables of interest
Aesthetics
x-axis
y-axis
colour
fill
size
labels
alpha
shape
line width
line type
Geometries point line histogram bar boxplot
Facets columns rows
Statistics binning smoothing descriptive inferential
Coordinates cartesian fixed polar limits
Themes Not data, but important for overall impact
Adapted from DataCamp
24. Relationship between cut and price?
Worse ——————————————————> Better
diamonds %>% ggplot(aes(x = cut, y = price)) +
geom_boxplot()
25. Relationship between cut and price?
[log-scale]
Worse ——————————————————> Better
diamonds %>% ggplot(aes(x = cut, y = price)) +
geom_boxplot()+ scale_y_log10()
Worse ——————————————————> Better
diamonds %>% ggplot(aes(x = cut, y = price)) +
geom_boxplot()
26. How many diamonds with each clarity?
Worse ——————————————————> Better
diamonds %>% ggplot(aes(x = clarity, fill = clarity))+geom_bar()
27. How many diamonds with each clarity?
Worse ——————————————————> Better
diamonds %>% ggplot(aes(x = clarity, fill = clarity))+geom_bar() diamonds %>% ggplot(aes(x = clarity, fill = clarity))
+geom_bar(width=1) + coord_polar()
28. Components of the grammar
Data Variables of interest
Aesthetics
x-axis
y-axis
colour
fill
size
labels
alpha
shape
line width
line type
Geometries point line histogram bar boxplot
Facets columns rows
Statistics binning smoothing descriptive inferential
Coordinates cartesian fixed polar limits
Themes Not data, but important for overall impact
Adapted from DataCamp
29. Finishing up with the diamonds data
diamonds %>% ggplot(aes(x = carat, y = price)) +
geom_point() + facet_grid(cut~clarity,
scales = "free_x") + geom_smooth(method = “lm")
diamonds %>% ggplot(aes(x = cut, y = price))
+ geom_boxplot()+ scale_y_log10()
30. Finishing up with the diamonds data
diamonds %>% ggplot(aes(x = carat, y = price)) +
geom_point() + facet_grid(cut~clarity,
scales = "free_x") + geom_smooth(method = "lm") + theme_bw()
diamonds %>% ggplot(aes(x = cut, y = price))
+ geom_boxplot()+ scale_y_log10() + theme_minimal()
31. Just changing the themediamonds %>% ggplot(aes(x = carat, y = price)) +
geom_point(col = "red") + facet_grid(cut~clarity,
scales = "free_x") + geom_smooth(method = "lm") +
theme_black()
diamonds %>% ggplot(aes(x = cut, y = price))
+ geom_boxplot()+ scale_y_log10() + theme_dark()
32. The Grammar of Graphics
Data Variables of interest
Aesthetics
x-axis
y-axis
colour
fill
size
labels
alpha
shape
line width
line type
Geometries point line histogram bar boxplot
Facets columns rows
Statistics binning smoothing descriptive inferential
Coordinates cartesian fixed polar limits
Themes Not data, but important for overall impact
35. The Grammar of Graphics
Data Variables of interest
Aesthetics
x-axis
y-axis
colour
fill
size
labels
alpha
shape
line width
line type
Geometries point line histogram bar boxplot
Facets columns rows
Statistics binning smoothing descriptive inferential
Coordinates cartesian fixed polar limits
Themes Not data, but important for overall impact
https://github.com/dvanic/18ResBazPresentation