R is a package for statistical analysis. It also does graphics. In Debian, at least install r-base.

Other useful pages:

Plotting

Plotting Arbitrary Functions

Using the curve function, one can plot arbitrary functions of the variable x. For example, from the rpy interface, you can plot sin(x) with the following:

   1 r('curve(sin(x), 0, 2*pi, ann=FALSE)')

The first argument of curve is the function to plot, followed by the start and end values along the x axis to evaluate at.

Using `matplot` to plot multiple series

Below is an example of the use of the matplot function so as not to have to always call r.par(new=True) in rpy.

   1 from rpy import *
   2 from Numeric import *
   3 import random
   4 
   5 # Data matrinces.                                                             
   6 A = zeros((10, 4))
   7 B = zeros((10, 4))
   8 
   9 # Create data.                                                                
  10 for i in range(len(A)):
  11     for j in range(len(A[0])):
  12         A[i, j] = random.randint(0, 10)
  13 
  14 for i in range(len(B)):
  15     for j in range(len(B[0])):
  16         B[i, j] = random.randint(0, 10)
  17 
  18 # Plot.                                                                       
  19 r.matplot(A, B, ann=False, pch=21)
  20 r.title("Using matplot", xlab='x', ylab='y')
  21 r.legend("topright", legend=('1', '2', '3', '4'), col=(1, 2, 3, 4), pch=21)
  22 raw_input("Press Enter to continue...")

Recovering from mistakes when interactively messing with plots

It's annoying that there is no easy way to undo changes you make to plots, besides doing it all over again. You can use dev.copy to copy what you have so far to a new X11 device, then play with that. If you mess up, you can turn that one off and go back to the old one. Some example code is below:

> plot(1:10, (1:10)**2, )                   # Plot to the default X11 device -> 2.
> dev.copy(X11)                             # Do the copy.  It is now active on device 3.
X11 
  3 
> title(main=expression(x ^ 2))             # Put in some information
> legend("topleft", "a simple data seies")  # oops, I misspelled that.
> dev.off()                                 # Turn off old device.
X11 
  2 
> dev.copy(X11)                             # copy it over again
X11 
  3 
> title(main=expression(x ^ 2))             # Do it right this time.
> legend("topleft", "a simple data series")

Data Conversion and Moving

`rpy` Interface

linear regression

calFrame <- data.frame(wnum = cal_energy, voltage=cal_voltage)
CalModel <- lm(wnum ~ voltage, data=calFrame) # make a linear model for v to Å          
print(coefficients(CalModel))
predict(CalModel, newdata=data.frame(voltage=c(0, .5, 1)))

Misc

Functions:

function( arglist )
 {  ...
    return(value)
 } 

Python to R

Python

R

list

vector

R (last edited 2008-03-10 01:38:55 by localhost)