Category Archives: ggplot2 – Introduction

ggplot2 – R way to Visualizing Data

I did an analysis on Delhi Assembly election – 2015 and published it on shiny apps, visit into the link and post your comments below. ggplot is an R package for data exploration and producing plots. It produces fantastic-looking graphics and allows one to slice and dice one’s data in many different ways.

First, install and include ggplot2


Suppose we have data set

df <- data.frame(year=c(2013,2014,2013,2014,2013,2014,2013,2014), region = c(“Region1″,”Region1″,”Region2”, “Region2″,”Region3”, “Region3″,”Region4”, “Region4”), rainfall=c(250, 221,124,267,348,321,289,329))

To plot a comapritive point graph, we can increase or decrease the size of point using “Size” option

ggplot(df, aes(region, rainfall, shape = region, color=year)) + geom_point(size = 5)



to plot a comapritive column bar graph, we use geom_bar

> ggplot(df, aes(region, rainfall, fill=as.factor(year)))+ geom_bar(stat=”identity”, width=.3,position=”dodge”)


To adjust the overlapping points on layers, we can use position=”stack” , “dodge” or “fill”.

Dodge – Adjust position by dodging overlaps to the side
Stack – One bar on the top of another
Fill – Stack type format but all the bar are of same height

>ggplot(df, aes(region, rainfall, fill=as.factor(year)))+ geom_bar(stat=”identity”, width=.3,position=”stack”)


>ggplot(df, aes(region, rainfall, fill=as.factor(year)))+ geom_bar(stat=”identity”, width=.3,position=”fill”)


To draw, line chart, we use geom_line()



Beautification of Plot

Number of of options provided by the ggplot2 to beautify the plot, we can labeling axis, title of the plot, legends.

>ggplot(df, aes(region, rainfall, group = year, color=as.factor(year))) + geom_line()+ scale_fill_manual(values = colors)+xlab(“Regions”)+ylab(“Rainfall in mm”)+ggtitle(“Rainfall in 4 Regions”)+theme(legend.position=”top”, legend.title=element_blank(), plot.title = element_text(lineheight=3, face=”bold”, color=”black”, size=14))


Data Spliting

Another powerfull feature of ffplot, split the data into multiple plots, we can use facets

>ggplot(r1, aes(variable, value, fill=as.factor(variable)))+ geom_bar(stat=”identity”, width=.3,position=”dodge”) +facet_wrap(~region)+xlab(“Time Periods”)+ylab(“Rainfall in mm”)+ggtitle(“Rainfall Distribution”)+theme(legend.position=”right”, legend.title=element_blank())

multiple Rplot


1 Comment

Posted by on March 19, 2015 in ggplot2, ggplot2 - Introduction


Tags: , , , , , ,