# R para principiantes # PUCE, Quito, 5-8 enero 2010 # Simon Queenborough # MIERCOLES: clase 6 # GRAFICOS BASICOS ########################## plot ################################# # To quantify biodiversity, species richness was calculated. In a statistical # analysis, we may want to model richness as a function of BARESOIL (or any of # the other soil and climate variables). Suppose we want to make a plot of species # richness versus the substrate variable ‘‘exposed soil,’’ denoted by BARESOIL. # The R commands to create such a graph is setwd("c:/R_Quito/data/") Veg <- read.table(file = "Vegetation2.txt", header = TRUE) plot(Veg$BARESOIL, Veg$R) # To import a graph from R into Microsoft Word, right-click on the graph in R to copy # and paste it into Word, or save it as a metafile (recommended, as it produces a good quality # graph), bitmap, or postscript file. This will also work with non-Windows operating systems. # Importing the latter two formats into Word is more complicated. # The first argument of the # plot command is displayed on the horizontal axis with the second argument # along the vertical axis. Richness, in this case, is the response, or dependent, # variable, and BARESOIL is the explanatory, or independent, variable. It is # conventional to plot the response variable along the vertical axis and the # explanatory variable along the horizontal axis. Be aware that for some # statistical functions in R, you must specify the response variable first, fol- # lowed by the explanatory variables. You would not be the first to accidentally type plot(Veg$R, Veg$BARESOIL) # and discover that the order should have been reversed. Alternatively, you can use plot(x = Veg$BARESOIL, y = Veg$R) # to avoid confusion over which variables will be plotted on the x-axis (horizontal) # and which on the y-axis (vertical). # The plot function does have a data argument, but we cannot use plot(BARESOIL, R, data = Veg) plot(R ~ BARESOIL, data = Veg) # does work # The most common modifications to any graph are adding a title and x- and # y-labels and setting the x- and y-limits. This is # accomplished by extending the plot command: plot(x = Veg$BARESOIL, y = Veg$R, xlab = "Exposed soil", ylab = "Species richness", main = "Scatter plot", xlim = c(0, 45), ylim = c(4, 19)) # The order in which xlab, ylab , main, xlim,and ylim are entered is # irrelevant, but they must be in lowercase letters. The xlab and ylab options # are used for labels and the main option for the title. The xlim and ylim options # are used to specify the lower and upper limits on the axes. You can also use xlim = c(min(Veg$BARESOIL), max(Veg$BARESOIL)) # within the plot command, but, if there aremissing values in the plotted variable, # you should extend the min and max functions with the na.rm=TRUE option. # This produces xlim = c(min(Veg$BARESOIL, na.rm = TRUE), max(Veg$BARESOIL, na.rm = TRUE)) ####################### simbolos, colores y tamanos ########################################## ### simbolos # By default, the plot function uses open circles (open dots) as plotting char- # acters, but characters can be selected from about 20 additional symbols. The # plotting character is specified with the pch option in the plot function; its # default value is 1 (which is the open dot or circle). plot(x = Veg$BARESOIL, y = Veg$R, xlab = "Exposed soil", ylab = "Species richness", main = "Scatter plot", xlim = c(0, 45), ylim = c(4, 19), pch = 16) # Make an empty chart plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type="n", ann=FALSE) # Plot digits 0-4 with increasing size and color text(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5) # Plot symbols 0-4 with increasing size and color points(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4) text((1:5)+0.4, rep(5,5), cex=0.6, (0:4)) # Plot symbols 5-9 with labels points(1:5, rep(4,5), cex=2, pch=(5:9)) text((1:5)+0.4, rep(4,5), cex=0.6, (5:9)) # Plot symbols 10-14 with labels points(1:5, rep(3,5), cex=2, pch=(10:14)) text((1:5)+0.4, rep(3,5), cex=0.6, (10:14)) # Plot symbols 15-19 with labels points(1:5, rep(2,5), cex=2, pch=(15:19)) text((1:5)+0.4, rep(2,5), cex=0.6, (15:19)) # Plot symbols 20-25 with labels points((1:6)*0.8+0.2, rep(1,6), cex=2, pch=(20:25)) text((1:6)*0.8+0.5, rep(1,6), cex=0.6, (20:25)) ### colores # The plotting option for changing colours is useful for graphics presented on a # screen or in a report, but is less so for scientific publications, as these are most # often printed in black and white. We recommend that you read Section 5.2.1 # before reading this section, as the procedure for colour is the same as that for symbols. # To replace the black dots in Fig. 5.2 with red, use plot(x = Veg$BARESOIL, y = Veg$R, xlab = "Exposed soil", ylab = "Species richness", main = "Scatter plot", xlim = c(0, 45), ylim = c(4, 19), col = 2) # For green, use col =3. Run the following code to see the other available colours. x <- 1:8 plot(x, col = x) # una lista de colores: # como hacer un palette de colores: # un vector de colores # You can also use a vector for the col option in the plot function. Suppose you # want to plot the observations from 1958 to 1974 as black filled squares and the # observations from1981 to 2002 as red filled circles (shown here as light grey). In # the previous section, you learned how to create filled squares and circles using # the variable Time2 with values 15 (square) and 16 (circle). Using two colours is # based on similar R code. First, create a new variable of the same length as # BARESOIL and richness R, which can be called Col2 . For those observations # from1958 to 1974, Col2 takes the value 1 (= black) and, for the following # years, 2 (= red). The R code is Veg$Time2 <- Veg$Time Veg$Time2 [Veg$Time <= 1974] <- 15 Veg$Time2 [Veg$Time > 1974] <- 16 Veg$Col2 <- Veg$Time Veg$Col2 [Veg$Time <= 1974] <- 1 Veg$Col2 [Veg$Time > 1974] <- 2 plot(x = Veg$BARESOIL, y = Veg$R, xlab = "Exposed soil", ylab = "Species richness", main = "Scatter plot", xlim = c(0, 45), ylim = c(4, 19), pch = Veg$Time2, col = Veg$Col2) #### tamano de simbolos # The size of the plotting symbols can be changed with the cex option, and again, # this can be added as an argument to the plot command. The default value for # cex is 1. Adding cex=1.5 to the plot command produces a graph in which all # points are 1.5 times the default size: plot(x = Veg$BARESOIL, y = Veg$R, xlab = "Exposed soil", ylab = "Species richness", main = "Scatter plot", xlim = c(0, 45), ylim = c(4, 19), pch = 16, cex = 1.5) ### usando un vector con 'cex' # As with the pch and col options, we demonstrate the use of a vector as the # argument of the cex option. Suppose you want to plot BARESOIL against species # richness using a large filled dot for observations made in 2002 and a smaller filled # dot for all other observations. Begin by creatinganewvectorwithvaluesof2for # observations made in 2002 and 1 for those from all other years. The values 1 and 2 # are good starting points for finding, through trial and error, the optimal size # difference. Try 3 and 1, 1.5 and 1, or 2 and 0.5, and so on, and decide which # looks best. Veg$Cex2 <- Veg$Time Veg$Cex2[Veg$Time == 2002] <- 2 Veg$Cex2[Veg$Time != 2002] <- 1 plot(x = Veg$BARESOIL, y = Veg$R, xlab = "Exposed soil", ylab = "Species richness", main = "Scatter plot", xlim = c(0, 45), ylim = c(4, 19), pch = 16, cex = Veg$Cex2) # Altering the symbol size can also be accomplished by using cex =1.5 * Veg$Cex2 # or cex =Veg$Cex2 /2.