ggplot2 in R

Created on December 13 2015 At 8:20 AM

Back To The Main Blog Page


This post will cover up most of the basic ggplot2 package in R . On his website ( package author Hadley Wickham describes ggplot2 as a plotting system for R, based on the grammar of graphics, which tries to take the good parts of base and lattice graphics and none of the bad parts. It takes care of many of the fiddly details that make plotting a hassle (like drawing legends) as well as providing a powerful model of graphics that makes it easy to produce complex multi-layered graphics. Grammar of Graphics is based on the concept of dividing a graph into 2 feartures namely : Data/Content and Aesthetics.This helps in a producing a graph rich with both content and handling beautiful aesthetics (i.e. the color of line,background etc.) very well even within a single line of code.


Unlike previous two plots ggplot doesn't come preinstalled (atleast not till this date).To install go to your R console and type install.packages("ggplot2")


qplot is one of the two ways to produce graph using ggplot. The exact syntax for the same is ::
qplot(x, y, data=, color=, shape=, size=, alpha=, geom=, method=, formula=, facets=, xlim=, ylim= xlab=, ylab=, main=, sub=) Here the variables share their usual meaning. Now we will use "mtcars" data to understand the functioning of this graph.

Unlike the other plots you can embed the aesthetics in the code itself like . To do this we use ggplot().


The syntax for ggplot() is the same . The only this is use of "+" to add features to the plot.
myPlot<-ggplot(mtcars,aes(x=mpg,y=disp))+ geom_point(aes(color=cyl))

The great feature of ggplot is the use of + operator . To map the clear linear trend just type :

For further reading read this link for documentation of ggplot2.

For any suggestion or comments feel free to contact me :: aaditya.jamuar [at]