R (programming language)

From CLAB

Jump to: navigation, search

The R programming language, sometimes described as GNU S, is a programming language and software environment for statistical computing and graphics.

Tutorials attempted locally:

Tools that use R:

[edit] Links

[edit] A Tutorial: Loading and plotting data with R

Original source

This tutorial demonstrates how to plot data loaded from files in R.

  • Given the data file data.txt located at <path>:
1970    45    63
1980    52    59
1990    59    52
2000    63    45
  • This data can easily be loaded into R using the following commands:
setwd("<path>")                # change working directory
data <- read.table("data.txt")  # load data
  • Plotting can then be performed as follows:
# 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) )
  • Export of the plot to pdf is easilty performed by adding pdf("filename.pdf") prior to the plotting, and dev.off() after the plotting.
  • Export to png can simmilarly be performed by calling png("filename.png") prior to the plotting, and dev.off() after the plotting. png("r_plot.png", width = 420, height = 340) will, for example yield the following figure:

Image:R_plot.png

[edit] radial.plot

As root:

install.packages("plotrix", dependencies = TRUE)

Then, to draw a radial plot:

j.sh:

R --no-save -q < j.R
convert -rotate 90 Rplots.ps Rplots.png

j.R:

require(plotrix)

testlen <- c(sin(seq(0,1.98*pi,length=100))+2+rnorm(100)/10)
testpos <- seq(0,1.98*pi,length=100)

radial.plot(testlen,testpos,rp.type="p",main="Test Polygon",line.col="blue")

Image:r.radial.plot.png