Chapter 3 Data Visualization
- Geometric shapes
- Multiple smoothing lines
- Statistical transformations
Today we are going to try some more code from Chapter 3 Data Visualization.
To start we will load the tidyverse. Note that ggplot2 is the first package loaded!
library(tidyverse)
We will continue to work with the mpg dataset that is in the ggplot2 package.
mpg
Make the scatterplot along with the smoothing line.
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy))
Multiple smoothing lines.
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv))
Statistical transformations
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
Proportions
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
Position adjustment
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
LS0tCnRpdGxlOiAiRXhwbG9yZSBhbmQgVmlzdWFsaXplMiIKYXV0aG9yOiAiUHJvZi4gRXJpYyBBLiBTdWVzcyIKb3V0cHV0OgogIHdvcmRfZG9jdW1lbnQ6IGRlZmF1bHQKICBodG1sX2RvY3VtZW50OgogICAgZGZfcHJpbnQ6IHBhZ2VkCiAgaHRtbF9ub3RlYm9vazogZGVmYXVsdAogIHBkZl9kb2N1bWVudDogZGVmYXVsdAotLS0KCiMgQ2hhcHRlciAzIERhdGEgVmlzdWFsaXphdGlvbgoKNC4gR2VvbWV0cmljIHNoYXBlcwo1LiBNdWx0aXBsZSBzbW9vdGhpbmcgbGluZXMKNi4gU3RhdGlzdGljYWwgdHJhbnNmb3JtYXRpb25zCgpUb2RheSB3ZSBhcmUgZ29pbmcgdG8gdHJ5IHNvbWUgbW9yZSBjb2RlIGZyb20gQ2hhcHRlciAzIERhdGEgVmlzdWFsaXphdGlvbi4KClRvIHN0YXJ0IHdlIHdpbGwgbG9hZCB0aGUgdGlkeXZlcnNlLiAgTm90ZSB0aGF0ICpnZ3Bsb3QyKiBpcyB0aGUgZmlyc3QgcGFja2FnZSBsb2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