ggplot2 is an alternative (and awesome) to base R for data visualization. It is based on The Grammar of Graphics. In this post, we will understand the philosophy behind ggplot2 and learn to build some of the most frequently used plots for visualizing data.
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.1
Grammar of Graphics is a formal system for building plots. The core idea is that any plot can be uniquely described as a combination of
ggplot(data = mtcars)
ggplot(data = mtcars) +
geom_point(mapping = aes(x = disp, y = mpg))
ggplot(data = mtcars) +
geom_point(mapping = aes(x = disp, y = mpg))
ggplot(data = mtcars) +
geom_point(mapping = aes(x = disp, y = mpg), stat = "identity")
ggplot(data = mtcars) +
geom_point(mapping = aes(x = disp, y = mpg), position = "identity")
ggplot(data = mtcars) +
geom_point(mapping = aes(x = disp, y = mpg), position = "identity") +
coord_cartesian()
ggplot(data = mtcars) +
geom_point(mapping = aes(x = disp, y = mpg), position = "identity") +
coord_cartesian() + facet_grid(. ~ gear)