Stacked Barplot in R and placing legend outside the graph area

Stacked Barplot_Rplot

R is on of the most widely used software for statistical analysis of data, which I use a lot for my work as a hydrogeologist /geochemist. R enables me to do what I want with the graphs, the exact way I like to present something. If you are not getting exactly what you want with excel and really likes to code your program, R is for you.

This post is about how to use R to make Stack Plots / stacked bar plots and place the legend of your stacks properly. I will also use the same data to make box plots to show you a different view of the data set. Let’s first take a look at the data. Data are presented in the table below which consist of monthly rainfall at five different stations. Our goal is to compare the data visually and do some statistics.

[wpdm_category id=r-programs order_field=id order=desc]

Table 1: Rainfall data in inches

Column1 ST1 ST2 ST22 ST4 ST5
January 3.37 2.08 0.00 7.74 5.70
February 3.02 2.71 3.06 1.36 1.69
March 3.83 4.67 1.04 3.12 2.36
April 3.83 4.68 3.78 3.80 4.66
May 4.39 3.68 4.07 5.83 3.78
June 3.93 2.91 1.49 7.37 8.13
July 4.17 2.56 3.16 8.01 5.74
August 3.68 4.15 3.01 2.08 3.53
September 3.39 3.61 4.81 1.35 1.01
October 3.19 1.72 2.10 2.64 1.50
November 2.96 1.08 0.59 3.02 0.30
December 2.87 2.41 2.97 5.26 0.00

Like any most other R applications, we like to read the data from a csv file, make a data-frame and then do different statistical analysis. please visit http://www.coalgeology.com/r-programming/ for many useful codes if you are just starting with R.

You can download the example database from the link StackBarPlotExample

Let me now present the code I have for the Stacked up box plot in R:

#Rainfall stackplot code in R
#Update the csv file to update new graph.
#coded on 2/10/2014
#Code by Ankan Basu, CPG, Hydrogeologist

# Read the CSV File into a dataframe
rf.full=read.csv(“C:\\Users\\ankan.basu\\Desktop\\r-programming\\R-data\\StackBarPlotExample.csv”,header=T,sep=”,”,stringsAsFactors=FALSE)

#select numeric vectors for the rainfalls.
rf.num=rf.full[2:length(rf.full)]

#check structure of the selected vector data
str(rf.num)

#Assign screen design / graphs per page
par(mfrow=c(1,1))

#Assign empty space around the graphs
par(mar=c(6.5,4,4,6));

#to draw legen outside the axis, use the following command.
par(xpd = T)

### By itself, changing the top margin does not help.
### It is also vital to set the xpd parameter to T so that
### R will draw outside the main plot region

barplot(as.matrix(rf.num),
col=rainbow(12),
xlab=””,
ylab=”Rainfall (inches)”,
las=3 # use to make the xaxis labels vertical
)

legend(#”topright”,
#x=6,
#y=50,
6.2, 50, #set the x and y position of the legend.
#xjust=1,
yjust=1,
#adj=0,
legend=c(“Jan”,”Feb”,”Mar”,”Apr”,”May”,”June”,”July”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”),
cex=.8,
#pch=c(22,0,0),
fill=rainbow(12)
#beside = TRUE)
)
#legend.text = TRUE,
#args.legend = list(x = “topright”, bty = “n”)

grid()
#legend(“topleft”, inset=.0, legend=”Box Plot for Pumps”)

#axis(2, at = 0:5, labels = 0:5)
#legend(“topright”, colnames(rf.num), fill = colors, bty = “n”)

 

If you run the code in R, this is the output you would get:

Stacked Barplot_Rplot

Stacked Barplot_Rplot

 

I have use twelve different colors to present twelve different months.

BOXPLOT in R:

Now, lets use the same database to create boxplots that would show us the spread in our database and produce the 5 point summary statistics in a graphical form.

Here is the R code:

#R code for Boxplot
#Coded by Ankan Basu, Hydrogeologist
#2/10/2014
rf.full=read.csv(“C:\\Users\\ankan.basu\\Desktop\\r-programming\\R-data\\StackBarPlotExample.csv”,header=T,sep=”,”,stringsAsFactors=FALSE)
rf.num=rf.full[2:length(rf.full)]
str(pf.num)
par(mfrow=c(1,1))
par(mar=c(3.5,4,1,1));
boxplot(rf.num,
#main=”Pipe Flows at Various Pumps at Kimballton Mine”,
#xlab=”Number of Cylinders”,
ylab=”Gallons Per Minute (GPM)”,
names.arg=names(rf.num),
cex.axis=0.9,
las=3,
cex.lab=.9,
col.main=”Blue”,
cex.main=1.0,
col=rainbow(12),
#col=(c(“lightblue”)),
#you can add more colors here
#notch=TRUE,
outline=TRUE

)
grid()

Output of the Box plot code will produce the following graph:

box_plot_Rplot

box_plot_Rplot

Leave a Comment

Your email address will not be published. Required fields are marked *