Evan Girvetz Winkenwerder Introduction to Graphics in R © R Foundation, from
Introduction to Graphics in R Two main graphics packages –base graphics changes to layout fairly easy –lattice graphics more structured graphics produces, but changes to the basic layout can be more difficult
Base Graphics: plot() plot() generic function for plotting R objects points, lines, etc. > ?plot > plot(x=primates$Bodywt,y=primates$Brainwt) > plot(x=Bodywt,y=Brainwt, data = primates) > plot(Brainwt~Bodywt, data = primates) Get primates data set from class website lectures page (lecture 3) Look at help for plot
xlab(), ylab() > plot(Brainwt~Bodywt, data = primates, + xlab=“Body Weight (kg)”, + ylab=“Brain Weight (g)”) Add labels to the x and y axes
xlim(), ylim() > plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1400)) Change the x and y axis limits
points and lines The arguments for the commands points and lines can be used for the command plot Plot must be called to start a plot –Then points and lines can add to a plot > ?points
pch : point character
> plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1400), pch=20) Change the point character style
cex : Character expansion > plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1400), pch=20, + cex = 3) Change the point character size Make it 1.5 time larger
Graphing parameters: par() These graphing parameters apply to all graphs called after they are set, until the graphing parameters are changed. This allows you to only have to write the command once to apply to multiple graphs.
Save old graphing parameters > ? par#show help for par > par() #look at par values > oldPar <- par() #save defaults For example: > par(cex = 1.25) > par() > par(oldPar) #restore defaults
Graphing parameters: par() > par(cex=1.25) # character expansion > plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1400), pch = 20) Some graphing parameters can be changed using par …others must be changed using par
Graphing parameters: par() > par(cex=1.25, pch = 20) # character expansion > plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1500)) Note difference between cex in the plot command and cex in par (the par command changes the text size also)
Multiple Point Characters > plot(Brainwt~Bodywt, xlim=c(0,300), ylim=c(0,1500), pch=21:25) Note that this overrides the pch in par
legend() > plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1500), pch=21:25) > legend("topright", legend=row.names(primates), + pch=21:25)
Rotating Tick Labels Look at the las argument under the help for par –0: always parallel to the axis [default] –1: always horizontal –2: always perpendicular to the axis –3: always vertical.
Tick Label Alignment > par(las=1) > plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1400)) las controls the rotation of labels on both axes
Axis Properties > plot(Brainwt~Bodywt, xlim=c(0,300), ylim=c(0,1400), pch=c(1:5), yaxp=c(0,1200,4))
Use of Colors > palette() Note that there are only eight colors in the default palette > colors() But, there are 657 built in colors Download the R Color Chart from the resources page
Use of Colors > pointColors <- c(“red”, “orange”, + “green”, “blue”, “magenta”) > plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1400), pch = + 21:25, bg = pointColors)
Use of Colors > pointColors <- c(“red”, “orange”, + “green”, “blue”, “magenta”) > plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1400), + bg = pointColors) > legend("topright", legend=row.names(primates), + pt.bg= pointColors, pch = 20)
Labeling: text() > plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1400)) > ?text > text(x=primates$Bodywt, + y=primates$Brainwt, + labels=row.names(primates),pos = 4)
Labeling: text() > plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1400)) > text(primates$Brainwt~primates$Bodywt, + labels=row.names(primates),pos = 4)
Labeling: text() > plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1400)) > text(x=primates$Bodywt, + y=primates$Brainwt+c(-0.125,0,0,0.125,0) + *par()$cxy[2], labels= row.names(primates), + pos = 4)
Labeling: identify() > plot(Brainwt~Bodywt, data = primates, + xlim=c(0,300), ylim=c(0,1500)) > identify(primates$Bodywt, primates$Brainwt, + labels=row.names(primates), n=2) Now click near any two points to label them
Putting it All Together > plot(Brainwt~Bodywt, data = primates, xlim=c(0,300), ylim=c(0,1400),xlab="Body Weight (kg)", ylab="Brain Weight (g)",pch = 21:25, bg = pointColors, yaxp=c(0,1200,4)) > legend("topright", legend=row.names(primates), pt.bg= pointColors, pch = 21:25) > text(x=primates$Bodywt, y=primates$Brainwt+c( ,0,0,0.125,0) *par()$cxy[2], labels= row.names(primates), pos = 4)
Adding Additional Plots You can use points or lines to add an additional plot to this graph. > lines(x=c(50,100,150,200), + y=c(250,400,550,750), type ="b", + lwd = 3, lty = 2)
Adding Additional Plots: points You can use points or lines to add an additional plot to this graph. > points(x = c(150,200,250,300), + y = c(100,150,200,250))