1 Normal GGPLOT2 Graph

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)

1.1 Scatterplot

# 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")

1.2 Histogram

ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_histogram()

1.3 Boxplot

ggplot(iris, aes(x = Species, y = Sepal.Length, col = Species)) +
geom_boxplot()

2 Dynamic GGPLOT

with the library of plotly, you can create dynamic plots by putting ggplot codes inside the function ggplotly()

library(plotly)

2.1 Scatterplot

ggplotly(Base+geom_point())

2.2 Histogram

ggplotly(ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_histogram())

2.3 Boxplot

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