Show the code
# current directory
getwd()
# Importing VRI data
<- read.csv("VRI_data.csv", header = TRUE)
vri_data
# Listing all files in the directory
dir()
attach(vri_data)
The following video will describe further tools to help build figures and graphs in RStudio. This includes how to save figures in multiple formats.
Advanced Tools Slides: Advanced graphs pptx
Follow these steps:
Step 1: Import the data “VRI_data.csv” and rename it as vri_data. Step 2: Use the attach function to mask all variable names to use these names directly.
# current directory
getwd()
# Importing VRI data
<- read.csv("VRI_data.csv", header = TRUE)
vri_data
# Listing all files in the directory
dir()
attach(vri_data)
From the vri data, we want to compare the height trend of two species by looking at their graphs. For “PLC” species show the maximum height on the graph.
Step 1: Create a separate data set for each species Step 2: Specify the margin to add space for two graphs side-by-side Step 3: Plot these two graphs Step 4: Select the maximum height from the “PLC” species and show it on the graph.
# separating data by species
<- subset(vri_data, (SPECIES_CD_1 =='CW'))
d1<- subset(vri_data, (SPECIES_CD_1 =='PLC'))
d2
# controlling margins: specifying the margin using mai
par(mfrow=c(1,2),mai=c(0.9,1.1,0.5,0.3), cex=1.0 ) # mai is the margin
="Projected Height (m)"
ylabel="Projected Age (years)"
xlabel=c(0,70)
ylm=c(0,470)
xlm
=1.5 # axis level - fontsize
cxlb=1 # axis fontsize
cxaxis=2.5 # title font
maincx
=3 # age column number
age=4 # height column number
height=1 # size of points
ldcex=2 # line width of points, we are making it thicker to emphasis on this maximum height value
ld<- max(d2[,height])
maxd1<-subset(d2, PROJ_HEIGHT_1==maxd1)
m1
# graph 1
plot(d1[,age],d1[,height], type = "p", pch = 1, col = "black", main=" ", ylim=ylm, xlim=xlm, cex=ldcex, cex.lab=cxlb, cex.axis=cxaxis, xlab=xlabel, ylab=ylabel)
# graph 2
plot(d2[,age],d2[,height], type = "p", pch = 2, col = "blue", ylim=ylm, xlim=xlm, cex=ldcex, cex.lab=cxlb, cex.axis=cxaxis, xlab=xlabel, ylab=ylabel)
points(m1[,age],m1[,height], type = "p", pch = 6, col = "red", lwd=ld, cex=ldcex)
The side-by-side graphs you created above - we want to save it as an image in the folder where you kept this.RMD file. This is called “saving as a permanent” graph/file/image. We can save plots as permanent files in different formats in R. To save a plot as an image use the png()
function and specify the name of the file in quotes (file=“name_of_new_file”
), the with and height of the graph (with=
and height=
), units (units=
), and the resolution wanted for the plot (res=
). Then use the par()
function to define the number of subplots and margins. Finally, write the code for each of the graphs that you want to save and run all the chunks of code together. This will automatically create a png file with your graphs in your working directory.
png("Graph2.png", width=15, height=10, units = 'in', res=1200)
par(mfrow=c(1,2),mai=c(1.1,0.9,0.8,0.3), cex=1.0, xpd=NA) # mai is the margin
# graph 1
plot(d1[,age],d1[,height], type = "p", pch = 1, col = "black", main=" ", ylim=ylm, xlim=xlm, cex=ldcex, cex.lab=cxlb, cex.axis=cxaxis, xlab=xlabel, ylab=ylabel)
# graph 2
plot(d2[,age],d2[,height], type = "p", pch = 2, col = "blue", ylim=ylm, xlim=xlm, cex=ldcex, cex.lab=cxlb, cex.axis=cxaxis, xlab=xlabel, ylab=ylabel)
points(m1[,age],m1[,height], type = "p", pch = 6, col = "red", lwd=ld, cex=ldcex)
graphics.off()
You can also save your plots as pdf files by using the pdf()
function. To save multiple graphs use the par()
function and define the order of your plots by using the function plot()
. This will order the graphs from the top left to top right, bottom left, and bottom right in case of using four subplots. Use mtext()
to add subtitles to each of your plots if desired.
# Creating Data:
<- seq(0,3, length=100) # vector with 100 values from 0 to 3
x
# create three functions of x
<- x^2+1
y1 <- -x^2+9
y2 <- sqrt(x)
y3
# saving as a pdf file
pdf(file="Example_save.pdf")
par(mfrow = c(2, 2), # 2 x 2 pictures on one plot
mar = c(4,4,2,2), # gives # of lines of margin on four sides of plot
# c(bottom, left, top, right)
pty = "s", # square plotting region,
bg="lightyellow") # set background color
# top left:
plot(x,y1)
mtext("Function 1", side = 3, line = 0.2) # add title for top left panel
# top right:
plot(x,y2)
mtext("Function 2", side = 3, line = 0.2)
# bottom left:
plot(x,y3)
mtext("Function 3", side = 3, line = 0.2)
# bottom right:
plot(x,y1, axes=FALSE,
ylim=c(0,10), xlim=c(0,3),
xlab = "", ylab = "", type="n")
lines(x,y1, lty=2)
lines(x,y2, lty=1, lwd=2)
lines(x,y3, lty=4, lwd=3, col=2)
axis(1) # x-axis
axis(2) # y-axis
mtext("x", side = 1, line = 3)
mtext("y", side = 2, line = 3)
box() # add frame around graph
legend(0,6.5, c("function 1"," function 2","function 3"),
lty=c(2,1,4), lwd=c(1,2,3), col=c(1,1,2), cex = 0.9)
mtext("All three functions", side = 3, line = 0.2)
# You can also use:
#dev.off()
graphics.off()
# other examples are:
# postscript(file="filename.eps") Adobe PostScript file
# jpeg(file="filename.jpeg") JPEG bitmap file
We can add legends to our plot by using the legend()
function. In this function, we must first define the coordinates where we want to place our legend in the graph. We can do this by defining the specific coordinates in the x and y axes where we want to place the legend or by writing “toprigth”
or “topleft”
(e.g. legend(0, 3.1)
or legend(“toprigth”)
). Then, we define the name of items in the legend by using c()
(e. g. legend(“toprigth”, c(“item1”, “item2”))
). Finally, we can add more details to the graph by changing the colour or plot symbols and size. Make sure that the number of colors and symbols matches the number of items you want to show in your legend.
<- subset(vri_data, (SPECIES_CD_1 =='CW'))
d1<- subset(vri_data, (SPECIES_CD_1 =='PLC'))
d2
="Projected Height (m)"
ylabel="Projected Age (years)"
xlabel=c(0,25)
ylm=c(0,200)
xlm
=1.5 # axis level - fontsize
cxlb=1.5 # axis fontsize
cxaxis=1.5 # title font
maincx
=3 # age column number
age=4 # height column number
height=3 # size ofpoints
ldcex=2
ld<- max(d2[,height])
maxd1<-subset(d2, PROJ_HEIGHT_1==maxd1)
m1par(mfrow=c(1,1), pty="s")
# graph 1
plot(d2[,age],d2[,height], type = "p", pch = 20, col = "blue", ylim=ylm, xlim=xlm, cex=ldcex, cex.lab=cxlb, cex.axis=cxaxis, xlab=xlabel, ylab=ylabel)
points(m1[,age],m1[,height], type = "p", pch =11, col = "red", lwd=ld)
# Adding legend
legend(50, 20, c("Projected height: Species is 'CW'", "Maximum Projected Height"), col = c("blue", "red"), pch = c(20, 11), cex=.7, pt.cex=1.5)
# Legend location: another procedure
par(mfrow=c(1,1), pty="s") # mai is the margin
# graph 1
plot(d2[,age],d2[,height], type = "p", pch = 20, col = "blue", ylim=ylm, xlim=xlm, cex=ldcex, cex.lab=cxlb, cex.axis=cxaxis, xlab=xlabel, ylab=ylabel)
points(m1[,age],m1[,height], type = "p", pch =11, col = "red", lwd=ld)
legend("topright", c("Projected height: Species is 'CW'", "Maximum Projected Height"), col = c("blue", "red"), pch = c(20, 11), cex=1, pt.cex=ld)
To add extra lines to our plots we can use the abline()
function. In this function we must state the type of line that we want, vertical or horizontal, and the coordinates where we want the line in the plot (abline(v=coordinate_in_y_axis)
or abline(h(coordinate_in_x_axis))
). Line types can be specified using lty()
, use 0=blank lines, 1=solid lines(default), 2=dashed lines, 3=dotted lines, 4=dotdash lines, 5=longdash lines, and 6=twodash lines. Finally, add text to the graph using text()
, specify first the coordinates where you want the text, and then write what you want to show on the graph.
par(mfrow=c(1,1), pty="s") # mai is the margin
# graph 1
plot(d2[,age],d2[,height], type = "p", pch = 20, col = "blue", ylim=ylm, xlim=xlm, cex=ldcex, cex.lab=cxlb, cex.axis=cxaxis, xlab=xlabel, ylab=ylabel)
points(m1[,age],m1[,height], type = "p", pch =11, col = "red", lwd=ld)
legend("topright", c("Projected height: Species is 'CW'", "Maximum Projected Height"), col = c("blue", "red"), pch = c(20, 11), cex=1, pt.cex=ld)
# additional lines
# add a vertical line at x=2
abline(v=52, lty=2)
# add a horizontal line at y=0
abline(h=10)
# abline(a,b) draws a line of slope b and intercept a
# add text
text(120,15,"Maximum Height")
To add mathematical expressions use the expression()
function. Define the mathematical expressions on label axes between quotes and tildes ( ~ ) (e.g. expression( “Total volume(”~ m^3 ~“)”)
).
par(mfrow=c(1,1),mai=c(0.9,1.1,0.8,0.3), cex=1.0 ) # mai is the margin
="Projected Volume(m3)"
ylabel="Projected Age (years)"
xlabel=c(0,500)
ylm=c(0,470)
xlm
=2 # axis level - fontsize
cxlb=2.5 # axis fontsize
cxaxis=2.5 # title font
maincx
=3 # age column number
age=4 # height column number
height=2 # size ofpoints
ldcex=2
ld=2
cx=4
ft# graph 1
plot(d1[,age],d1[,height], type = "p", pch = 1, col = "black", main=" ", ylim=ylm, xlim=xlm, cex=ldcex, cex.lab=cxlb, cex.axis=cxaxis, xlab=xlabel, ylab="")
mtext(expression("Total volume ("~m^3~")"), side=2, line=3, las=0, cex=cx, font =ft)
If the character/categorical variable is not producing the summary table as shown on the video then try the following:
summary(as.factor(SPECIES_CD_1))
plot(as.factor(SPECIES_CD_1))