GGPLOT2 applies layer-by-layer concepts, all ggplots are created using the format “base + layer1 + layer2 + layer3 …”, See following charts for better understanding.
library(ggplot2)
# Create the plotting base with x,y axis
Base<-ggplot(iris, aes(x = Sepal.Length,
y = Petal.Length,
col = Species))
Base
# Add 1 layer of points to the base
Base+geom_point()
# Add 1 more layer to wrap species catogories in seperate facets
Base+geom_point()+ facet_wrap(~Species)
# Add 1 more layer to fit a smoothed line to each category of species
Base+geom_point()+ facet_wrap(~Species)+geom_smooth(method = "lm")
ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_histogram()
ggplot(iris, aes(x = Species, y = Sepal.Length, col = Species)) +
geom_boxplot()
with the library of plotly, you can create dynamic plots by putting ggplot codes inside the function ggplotly()
library(plotly)
ggplotly(Base+geom_point())
ggplotly(ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_histogram())
ggplotly(ggplot(iris, aes(x = Species, y = Sepal.Length, col = Species)) +
geom_boxplot())
For more info on plotly, visit https://plot.ly/ggplot2/
For more info on GGPLOT2,visit https://www.rstudio.com/wp-content/uploads/2015/12/ggplot2-cheatsheet-2.0.pdf