R Programming/Graphics

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents

[edit] Basics

[edit] plot

  • The main function is plot.
  • Plot opens a graph window and draw a graphics. A very basic example would be the following :
plot(0)
  • Plot deals with datasets, one continuous variable, two continuous variable, one discrete variable, etc.
    • One continuous variable : creates a time series plot
# Example with the anscombe dataset (included in the datasets package)
plot(anscombe$x1)
    • Two continous variables : creates a scatterplot. The first term is one the x-axis and the second one on the y-axis.
plot(anscombe$x1,anscombe$x2)
    • one dataset : create a scatterplot matrix
plot(anscombe) 

[edit] type

The type can be

  • n for none
  • p for points
  • l for lines
  • h for histogram-like
  • s/S for steps
  • see ?plot for more.

[edit] Symbols

  • Symbols can be changed using the pch option which takes integer values between 0 and 25.
plot(x1,x2,pch=0)
  • Note that symbols can also use a vector of integers.

In that case the symbol will be different for each observation.

plot(x1,x2,pch=c(1:10))
  • If you want to have a broad look at all symbols, you can do a simple loop over pch :
par(mfrow = c(4,3))
for (k in 0:11){
    plot(x1,x2,pch=k)
    }

[edit] Axes

  • The default output print the axes.
  • You can remove them :
attach(anscombe)
plot(x1,x2,axes=FALSE)
  • You can also change the axes
plot(x1,x2,axes=F)
axis(1,col="red",col.axis="blue",font.axis=3)
axis(2,col="red",col.axis="blue",font.axis=2,las=2)


[edit] Size

  • cex change the size of the output


[edit] Using graphics parameters

  • The par() command defines most of the parameters of a plot.

If you just type par() in R, you will se the whole list of default arguments


[edit] Colors

colors()

[edit] Exporting your graphs

  • CTRL + W : copy the current graph as a Window Metafile in the clipboard. You can paste it in another application.
  • CTRL + C : copy the current graph as a Bitmap file in the clipboard.
  • The Windows Metafile is richer than the Bitmap format.
  • Use the contextual menu (Right clic on Windows and Linux or Control + Clic on Mac)
  • Export of the plot to pdf is easily performed by adding pdf("filename.pdf") prior to the plotting, and dev.off() after the plotting.
  • Export to png can similarly be performed by calling png("filename.png") prior to the plotting, and dev.off() after the plotting.
  • Use the savePlot() function to save existing graphics

Here is an example of export to png format:

png("r_plot.png", width = 420, height = 340)
…
dev.off()

Here is an example of export to ps format:

postscript(file="W:/…/graph1.ps",horizontal=F,
pagecentre=F,paper="special",width=8.33,height=5.56) 
…
dev.off()


You can export to SVG:

#R version 2.3, www.r-project.org
library("RSvgDevice")
devSVG("histogram.svg", width = 5, height = 4)
set.seed(50)
par(mar=c(3,2,1,1),yaxs="i",mgp=c(1.8,.9,0))
hist(rnorm(100),col="lightblue",main="",xlab="",ylab="",ylim=c(0,28))
box()
dev.off()

Here is the output :

Histogram example.svg


[edit] Topics

[edit] Time series plot

[edit] Scatter plot

The first variable in the horizontal axis and the second one in the vertical axis

plot(x,y)

[edit] Overlaying scatterplots

If one wants to overlay two scatterplot, he can use the new=FALSE option in the par() command. Here is an example :

plot(effec_passe,p_3,type="p",ylab="Probabilité",xlab="Effectif d'origine")
par(new=TRUE,col="red") # creates a new graphic
plot(effec_passe,p_prev_3,axes=F,type="p",ylab="",xlab="")
par(new=FALSE,col="black") # does not create a new graphic

[edit] Regression line

[edit] Bar charts

[edit] Dot chart

[edit] Standard error bar chart

  • Standard error bar chart are very useful to plot estimates with information on confidence interval.
  • The Hmisc package has an errbar() function.
    • NOTE : The default output in errbar() changed between R version 2.8.1 and R version 2.9.2. Axis are not displayed by default anymore.
  • See also the coefplot() function in Gelman and Hill's "arm" package


[edit] Analytic Graphics

Alpha <- .3
S <- .16
N <- .005
Gamma <- .015
Mu <- .03
f1 <- function(k.e) S * k.e^Alpha 
f2 <- function(k.e) (N + Mu + Gamma)*k.e 
plot(0,type="n",xlim=c(0,8),ylim=c(0,.5),ylab="",xlab=(expression(k^e)))
curve(f1, add = T) 
curve(f2, add = T) 
text(7,f1(7), expression(s * k^e^alpha ))
text(7,f2(7), expression(paste("(",n + mu + gamma,")","*",k^e)))

[edit] Miscellanous

 # Setup empty plot window
 plot(0, type = "n", main = "Plot Title", xlab = "X axis", ylab = "Y axis", xlim = c(1970,2000), ylim = c(0,100))
 grid()                              # add grid
 lines(data[,1], data[,2], lty = 1)  # draw first dataset
 lines(data[,1], data[,3], lty = 2)  # draw second dataset
 legend("topright", c("first dataset", "second dataset"), lty = c(1, 2) )

[edit] Maps

[edit] References


Previous: Data Index Next: Descriptive Statistics