- Become aware of the various components that comprise a graph
- Understand how ggplot2 uses these components to construct a plot
Monday, February 2nd, 2015
Why is it necessary to understand the grammar?
library(ggplot2) head(diamonds)
## carat cut color clarity depth table price x y z ## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 ## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 ## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 ## 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63 ## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 ## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
ggplot(data=diamonds, aes(x = x, y = carat)) + geom_point()
ggplot(data=diamonds, aes(x = x, y = carat)) + geom_point()
ggplot() + layer(data = diamonds, mapping = aes(x = x, y = carat), geom = "point", stat = "identity", pos = "identity") + scale_x_continuous() + scale_y_continuous() + coord_cartesian() + theme()
ggplot(diamonds, aes(x,carat)) + geom_point()
What will this plot look like?
ggplot(data = economics, aes(x = date, y = pop)) + geom_line()
ggplot(data = economics, aes(x = date, y = pop)) + geom_line()
ggplot(data = diamonds, aes(x = price)) + geom_histogram()
ggplot(data = diamonds, aes(x = price)) + geom_histogram()
ggplot(data = diamonds, aes(x = price)) + geom_histogram(aes(y = ..density..)) + geom_density(color = "red")
ggplot(data=diamonds, aes(x = clarity, fill = cut)) + geom_bar()
library(RColorBrewer) ggplot(data=diamonds, aes(x = clarity, fill = cut)) + geom_bar() + scale_fill_brewer()
ggplot(data=diamonds, aes(x = clarity, fill = cut)) + geom_bar() + scale_fill_brewer() + coord_flip()
ggplot(data=diamonds, aes(x = clarity, fill = cut)) + geom_bar(position = "dodge") + scale_fill_brewer()
ggplot(data=diamonds, aes(x = clarity, fill = cut)) + geom_bar() + scale_fill_brewer() + facet_wrap(~cut)
ggplot(data=diamonds, aes(x = clarity, fill = cut)) + geom_bar() + scale_fill_brewer() + facet_wrap(~cut) + theme_bw()
ggplot(df, aes(x = "", y = value, fill = variable)) + geom_bar(width = 1, stat = "identity") + scale_fill_manual(values = c("red", "yellow")) + coord_polar("y", start = pi / 3) + labs(title = "Pac man")