############################## ## Graphics - R Tutorial ## ############################## # Please read the tutorial's general instructions first, and prepare # for loading an external datafile, if you plan to use one. # The content of this file can be pasted directly into the R console. # It should blurp lost of errors only at the point where you # are supposed to have an external dataset available. # However, it is much handier to use the "display file" command # within the R menu to look at this file, and paste command by command # to the console, using control-V. Take your time to experiment a bit # with the listed commands. #--------------------------------------- # .Rgraphics: Making graphics within R. #--------------------------------------- #! A warning: There is no manual available that explains #! all the intricacies of graphics in R. Usually, you proceed #! by experimenting, and by modifying graphics scripts #! that you find here and there. # -------------------------- # | Bar plots, Histograms | # -------------------------- # Make a dataset with counts. counts<-c(2,3,4,5,5,1,1,2,3,4,4,5,5) # Make a barplot of the content. barplot(counts) # This is not very insightful, or it does not really summarize. # We are better of if we used summarized data, such as: table(counts) # and plot that barplot(table(counts)) # We could also plot proportions: barplot(table(counts)/length(counts)) # It is tempting to use the histogram function hist(), # but as you can see, it collapses the two lowest classes of counts. # There is of course the possbility to foddle with the options in hist() # in order to obtain the same result, # but barplot seems easier to use in this case. hist(counts) # Uses raw frequencies, which are counts. hist(counts,freq=FALSE) # uses relative frequencies or proportions # Let's now take a look at some of the examples in help(barplot) tN <- table(rpois(100, lambda=5)) # draw Poisson random numbers barplot(tN, col='gray') # As you see, it is possible to specify the color of the bars. barplot(tN, space = 1.5, axisnames=FALSE) # This illustrates some further options. # And now some showcases from the help file # There is an extensive amount of options available, # as you will notice data(VADeaths, package = "base") tot<-colMeans(VADeaths) tot bp<-barplot(VADeaths) # default, with stacked bars text(bp, tot + 3, format(tot), xpd = TRUE, col = "blue") # If we want to have bars within groups next to each other, we # set the beside option to "TRUE". barplot(VADeaths, beside = TRUE, col = c("lightblue", "mistyrose", "lightcyan","lavender", "cornsilk"), legend = rownames(VADeaths), ylim = c(0, 100)) # notice that a single command can extend over several lines title(main = "Death Rates in Virginia", font.main = 4) # We now exchange rows for columns to make another plot, # using the transpose command: hh <- t(VADeaths) # We want to select the five last columns and reverse the order: hh<-hh[,1:5] mybarcol <- "gray20" # my favourite colour, really... # It is sometimes handy to save a barplot as an object. # This objecct does not contain all information # about the plot, as you will soon see. plotobj<-barplot(hh, beside = TRUE, col = c("lightblue", "mistyrose", "lightcyan", "lavender"), legend = colnames(VADeaths), ylim= c(0,100), main = "Death Rates in Virginia", font.main = 4, sub = " Faked upper 2*sigma error bars", col.sub = mybarcol,cex.names = 1.5) # Those faked upper error bars, where are they then? # run the following line to add them: segments(plotobj, hh, plotobj, hh + 2*sqrt(1000*hh/100), col = mybarcol, lwd = 1.5) # the plotobj object contains (partial!)graphical information # on bar locations along the horizontal axis, which we used # to draw the lines segments() produced. plotobj # Try this, for fun, segments(plotobj, hh, plotobj+0.2, hh + 2*sqrt(1000*hh/100), col = mybarcol, lwd = 3) help(segments) # ------------------------- # | Box and whiskers plot | # ------------------------- # An example on or "counts" vector, constructed above: boxplot(counts) # Try to add the horizontal=TRUE optionand look at the effect. # The following example is a bit more intricate. data(InsectSprays) summary(InsectSprays) boxplot(count ~ spray, data = InsectSprays, col = "lightgray") # The ~ notation is usually used to denote a formula. # Here it says: counts depend on spray type, # therefore we get a separate box-whiskers plot per spray type. # -------------------- # | The plot command | # -------------------- # Let's first use the plot() function to make a scatterplot # We do this on the dataset cars, that contains only two variables data(cars) summary(cars) plot(cars) # There are different ways to steer plot(). Compare plot(cars$dist,cars$speed) # The first argument, dist, gets the horizontal axis, # the second one, speed, the vertical: plot(cars$dist~cars$speed) # The ~ notation is usually used to denote a formula. # Here it says: distance depends on speed, # therefore the response variable, distance, # ends up along the vertical axis. # You can also add output from a model fit to the plot. # Typing the following line adds a # fitted "lowess" regression line: lines(lowess(cars)) # This also shows that it is possible to add elements to a plot # as long as the extra graphical window is active. # -------- # | Demo | # -------- # Finally, you can run demo(graphics) to get an overview # of many other possible plot types and their options # You can simply type "q" at the command line to quit it. demo(graphics) # ------------------------------------------------------------- # When finished running this file, please continue with # the ".Rclassics" or one of the more advanced tutorial files. # There are more examples of specific graphics on those. # ------------------------------------------------------------- # Much of the material in this short tutorial comes from # Using R for data analysis and graphics. An Introduction # by J. H. Maindonald (2001) # Using R for introductory Statistics # by John Verzani (2002) # both are acessible via links on the documentation section of # the R-project website. # Tom Van Dooren, version 17/10/2002