Show the code
# VRI data
<-read.csv("VRI_data.csv",header = T)
vri_data
attach(vri_data)
# separating data by species
<- subset(vri_data, (SPECIES_CD_1 =='CW'))
d1<- subset(vri_data, (SPECIES_CD_1 =='PLC')) d2
Barplot Slides: RStudio Barplots pptx
The following video will describe how to create barplots in RStudio. Barplots are an effective way to quickly analyse your data.
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
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.
# VRI data
<-read.csv("VRI_data.csv",header = T)
vri_data
attach(vri_data)
# separating data by species
<- subset(vri_data, (SPECIES_CD_1 =='CW'))
d1<- subset(vri_data, (SPECIES_CD_1 =='PLC')) d2
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.
="Projected Height (m)"
ylabel="Projected Age (years)"
xlabel=c(0,60)
ylm=c(0,470)
xlm
=1.1 # axis level - fontsize
cxlb=1.1 # axis fontsize
cxaxis=2.5 # title font
maincx
=3 # age column number
row1=4 # ht column number
row2=1 # size ofpoints
ldcex=1
ld<- max(d2[,row2])
maxd1<-subset(d2, PROJ_HEIGHT_1==maxd1)
m1<- max(d1[,row2])
maxd2<-subset(d1, PROJ_HEIGHT_1==maxd2)
m2#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()
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:
class()
function for checking on this. table()
to create a vector with the counts of the number of items that you have in the CC column. 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.# .txt data file
<- read.table("trees.txt", header = TRUE)
trees # checking the data:
dim(trees)
names(trees)
# attached all variables
attach(trees)
# crown class variable and create a table
class(CC)
<- table(CC)
counts
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)
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=
.
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
<- table(SPECIES_CD_1)
leading_spec <- (table(SPECIES_CD_1)*100)/2700
percent_spec 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)
VRI Data CSV: VRI_data.csv
VRI RData: VRI_data.RData
Trees text file: trees.txt
Trees RData: trees.RData