ggplot2 also termed as Grammer of Graphics is a free, opensource and easy to use visualization package widely used in R. It is the most powerful visualization package written by Hadley Wickham.
It includes several layers on which it is governed. The layers are as follows:

Building Blocks of layers
Layers with variables of interest are as follows:
- Aesthetics: x axis, y axis, color, fill, size, labels, alpha, shape, line width, line type
- Geometrics: point, line, histogram, bar, boxplot
- Facets: Columns, rows
- Statistics: Binning, smoothing, descriptive, intermediate
- Coordinates: Cartesian, fixed, polar, limits
- Themes: Non data link
The Dataset
mtcars(motor trend car road test) comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles and comes pre installed with dplyr package in R.
# Installing the packageinstall.packages("dplyr") # Loading packagelibrary(dplyr) # Summary of dataset in packagesummary(mtcars) |
Performing ggplot2 on dataset
We devise visualisations on mtcars dataset which includes 32 car brands and 11 attributes using ggplot2 layers.
# Installing the packageinstall.packages("ggplot2") # Loading packageslibrary(ggplot2)library(dplyr) # Data Layerggplot(data = mtcars) # Aesthetic Layerggplot(data = mtcars, aes(x = hp, y = mpg, col = disp)) # Geometric layerggplot(data = mtcars, aes(x = hp, y = mpg, col = disp)) + geom_point() # Adding sizeggplot(data = mtcars, aes(x = hp, y = mpg, size = disp)) + geom_point() # Adding color and shapeggplot(data = mtcars, aes(x = hp, y = mpg, col = factor(cyl), shape = factor(am))) + geom_point() # Histogram plotggplot(data = mtcars, aes(x = hp)) + geom_histogram(binwidth = 5) # Facet Layerp <- ggplot(data = mtcars, aes(x = hp, y = mpg, shape = factor(cyl))) + geom_point() # Separate rows according to transmission typep + facet_grid(am ~ .) # Separate columns according to cylindersp + facet_grid(. ~ cyl) # Statistics layerggplot(data = mtcars, aes(x = hp, y = mpg)) + geom_point() + stat_smooth(method = lm, col = "red") # Coordinates layer: Control plot dimensionsggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point() + stat_smooth(method = lm, col = "red") + scale_y_continuous("mpg", limits = c(2, 35), expand = c(0, 0)) + scale_x_continuous("wt", limits = c(0, 25), expand = c(0, 0)) + coord_equal() # Add coord_cartesian() to proper zoom inggplot(data = mtcars, aes(x = wt, y = hp, col = am)) + geom_point() + geom_smooth() + coord_cartesian(xlim = c(3, 6)) # Theme layerggplot(data = mtcars, aes(x = hp, y = mpg)) + geom_point() + facet_grid(. ~ cyl) + theme(plot.background = element_rect( fill = "black", colour = "gray")) ggplot(data = mtcars, aes(x = hp, y = mpg)) + geom_point() + facet_grid(am ~ cyl) + theme_gray() |
Outputs:
- Geometric layer

- Geometric layer – Adding Size

- Geometric layer – Adding colour and shape

- Geometric layer – Histogram plot

- Facet layer – Separate rows according to transmission type

- Facet layer – Separate columns according to cylinders

- Statistics Layer

- Coordinates layer: Control plot dimensions

- Coord_cartesian() to proper zoom in

- Theme layer – element_rect() function

- Theme layer

ggplot2provides various types of visualizations. More parameters can be used included in the package as the package gives greater control over the visualizations of data. Many packages can integrate with the ggplot2 package to make the visualizations interactive and animated.


