What is GGPlot2 & How To Use It

Sharing is Caring

Machine learning & Data Science course

Are you interested in ggplot2? Or machine learning, data visualization, r programming? Join one of the most comprehensive & affordable machine learning & data science course.

Ggplot2 is an advance R library designed for creating charts. Ggplot2 is one of the 3 graphics system in R, other than base and lattice. it was written by Hadley Wickham.

Web site: http://ggplot2.org

GGplot2 Overview

GGplot2 is like a R grammar system for graphics where the syntax consists various parts of graphics and you can build graphs using this syntax provided by ggplot2. Its like mapping various attributes of graphs in R, it addresses various components of graphs and stitch them together to a build complete chart. These are important elements of Graphs in ggplot2 in R;

· Data
· Aesthetics like shape, size, and color
· Geometic objects like points, lines etc

Broadly it is similar to other graphics systems in R, it will look for the data in data frame or environment and graphs are made up of aesthetics (shape,size, color) and geoms (points, lines). GGPlot2 consist for 2 graph functions and they are;

  • ggplot
  • qplot

ggplot vs qplot

As mentioned above, these are two ggplot2 functions which are used to create graphs in R using ggplot2 graph system. The utilization of either of them depends upon the complexity & requirement of the project.

· ggplot is used for larger complex data

· qplot which is used for simpler data sets

ggplot() is the core function and it is used when qplot is not sufficient whereas qplot is used when you are not looking at too much of functionality.

Like other R libraries, you first need to install ggplot2 library

After installing, Initiate ggplot2 in R studio


let’s use the in-built dataset for qplot example;

initiate mpg dataframe in R studio by typing;


which will be followed by this;


Not type following command in R studio;


  • where displ is the x coordinate
  • hwy is the y coordinate
  • mpg is the data frame

you can replace these variables in your graph to get desired result. Above code will generate following graph;

qplot graph with mpg dataset

ggplot function

ggplot accepts and need following elements in the command;

•  A data frame

•  aestheic: details of data mapped to aesthetics like color, size

•  geoms: points, lines, shapes.

•  facets: in case of conditional plots.

•  stats: quantiles, smoothing.

•  scales: male = red, female = blue etc

•  coordinate system

Let’s use the above mentioned data set for ggplot example, in this case we will first create objects for each activity and then use them to print graph;


It will generate following graph;

ggplot graph in R studio

In upcoming post, we will see more graphs using ggplot2 and other graphics system in R. If you have any question or suggestion, please leave your comment.

Sharing is Caring
About akhilendra

Hi, I’m Akhilendra and I write about Business Analysis, Data Science, IT & Web. Join me on Twitter, Facebook & Google+

Speak Your Mind