6.3 Barplot in R Markdown

Slides:

Barplot Slides: RStudio Barplots pptx

Video: Barplot in R Markdown

The following video will describe how to create barplots in RStudio. Barplots are an effective way to quickly analyse your data.

Note

Due to recent changes in the R version some lines of code that are shown in the video are not longer compatible. To see the current working code please see the .RMD and knitted files

6.3.1 Preparing directory and data setup

In this lecture, we will be creating side-by-side graphs for two different species using the vri.csv file. For doing this use the subset() and par() functions such as were explained in previous lectures.

Show the code
# VRI data

vri_data<-read.csv("VRI_data.csv",header = T)

attach(vri_data)

# separating data by species
d1<- subset(vri_data, (SPECIES_CD_1 =='CW'))
d2<- subset(vri_data, (SPECIES_CD_1 =='PLC'))

6.3.2 Add one legend for multiple graphs

We can add a legend for multiple graphs by using the legend() function. In this case, we have to use the last graph created as a reference to add the coordinates of the legend. Make sure that you give appropriate margin space in the graph for adding the legend using the mai= option in the par() function. Use xpd=NA to specify that we want the legend outside of the graph area.

Show the code

ylabel="Projected Height (m)"
xlabel="Projected Age (years)"
ylm=c(0,60)
xlm=c(0,470)

cxlb=1.1 # axis level - fontsize
cxaxis=1.1 # axis fontsize
maincx=2.5 # title font

row1=3 # age column number
row2=4 # ht column number
ldcex=1 # size ofpoints
ld=1
maxd1<- max(d2[,row2])
m1<-subset(d2, PROJ_HEIGHT_1==maxd1)
maxd2<- max(d1[,row2])
m2<-subset(d1, PROJ_HEIGHT_1==maxd2)
#names(vri_data)

# creating a permanent image file
png("One_legend.png", width=5, height=5, units = 'in', res=1200)
par(mfrow=c(1,2),mai=c(3,0.9,0.2,0.3), cex=1.0, xpd=NA) # mai is the margin

# graph 1
plot(d1[,row1],d1[,row2], type = "p", pch = 20, col = "blue",  ylim=ylm, xlim=xlm, cex=ldcex, cex.lab=cxlb, cex.axis=cxaxis,  xlab=xlabel, ylab=ylabel)
points(m2[,row1],m2[,row2], type = "p", pch =11, col = "red", lwd=ld)

# graph 2
plot(d2[,row1],d2[,row2], type = "p", pch = 20, col = "black",  ylim=ylm, xlim=xlm, cex=ldcex, cex.lab=cxlb, cex.axis=cxaxis,  xlab=xlabel, ylab=ylabel)
points(m1[,row1],m1[,row2], type = "p", pch =11, col = "red", lwd=ld)

legend(-700, -35, ncol=1 ,c("Projected height: Species is 'CW'", "Projected height: Species is 'PLC'", "Maximum Projected Height"), col = c("blue", "black", "red"), pch = c(20, 20, 11), cex=1, pt.cex=ld)

graphics.off()

6.3.3 Creating barplots

For this section we will create a bar plot using the trees.txt file. Use the read.table() function for importing this file into R. Then:

  1. Determine the type of variables that you have (categorical or numerical). In this case we want to know what type of variable is “CC”. Use the class() function for checking on this.
  2. Use table() to create a vector with the counts of the number of items that you have in the CC column.
  3. Use barplot() function to create a bar plot. Use the vector that you create with the counts as a file and modify graph limits, colours, and labels if desired.
Show the code
# .txt data file
trees <- read.table("trees.txt", header = TRUE)
# checking the data:
dim(trees)
names(trees)
# attached all variables
attach(trees)

# crown class variable and create a table
class(CC)
counts <- table(CC)
counts

#barplot 1
par(mfrow=c(1,1),mai=c(1,0.9,0.2,0.3), cex=1.0, oma=c(5,2,2,0), xpd=NA) # mai is the margin
barplot(counts)
graphics.off()

#barplot 2
graphics.off()
par(mfrow=c(1,1),mai=c(1,0.9,0.2,0.3), cex=1.0, oma=c(5,2,2,0), xpd=NA) # mai is the margin
barplot(counts, ylim = c(0, 20))

#barplot 3
graphics.off()
par(mfrow=c(1,1),mai=c(1,0.9,0.2,0.3), cex=1.0, oma=c(5,2,2,0), xpd=NA) # mai is the margin
barplot(counts, ylim = c(0, 20), ylab = "Frequency", xlab = "Crown Classes")

detach(trees)

6.3.4 VRI data bar chart

We can also create bar plots by selecting a categorical variable and using the plot() function. In this case, R will automatically create a bar plot with the selected categorical variable (e.g. plot (SPECIES_CD_1) ). To change the direction of the numbers in the y axis use las=1 into barplot(). To change the names in the x axis use names.arg= c(“name_variable1”, “name_variable2”). This will change the names that you have for each column in the x axis. To modify the font size use cex.names=.

Show the code
attach(vri_data)
class(SPECIES_CD_1)
 
#barplot 1
#coverting the character variable as a factor to plot it

plot(factor(SPECIES_CD_1))

#barplot 2
leading_spec <- table(SPECIES_CD_1)
percent_spec <- (table(SPECIES_CD_1)*100)/2700
barplot(percent_spec)

#barplot 3
barplot(percent_spec, main="Leading Species of Stands",     xlab="Species", ylab="%")

#barplot 4: rotate y axis values:

barplot(percent_spec, main="Leading Species of Stands",     xlab="Species", ylab="%", las=1)

#barplot 5
# set the names of categories under x axis:
barplot(percent_spec, main="Leading Species of Stands",     xlab="Species", ylab="%", las=1, names.arg=c("non-treed stands",  "Western Red Cedar",  "Alder", "Western Hemlock", "Lodgepole pine",  "Sitka Spruce", "Yellow  Cedar"))

# barplot 6: reduce the names to adjust the space
barplot(percent_spec, main="Leading Species of Stands",     xlab="Species", ylab="%", las=1, names.arg=c("Non-treed",  "Western R. Cedar",  "Alder", "Western Hemlock", "L. pine",  "Sitka Spruce", "Yellow     Cedar"), ylim=c(0,60), cex.axis=1.5, cex.lab=1.5, cex.names=0.7)
Data Files:

VRI Data CSV: VRI_data.csv

VRI RData: VRI_data.RData

Trees text file: trees.txt

Trees RData: trees.RData