R is a package for statistical analysis. It also does graphics. In Debian, at least install rbase.
Other useful pages:
Plotting
axTicks  generate list of locations to put axis ticks.
Add more axis ticks: axis(side=2, at=axTicks(side=2, axp=c(LOW,HIGH,NUM)))... ok, so this isn't really ideal.
Tick marks inside: To set the default, use par(tcl=.5).
To set up the entire plot area yourself, in preparation for using lines instead of plot, do: plot.new(); plot.window(xlim=, ylim=); title(main=, sub=, xlab=, ylab=); box(); axis(1); axis(2)
To plot math equations, see ?plotmath. In short, for any plot command, enclose your formula in expression. When you are doing this from python, use plot(..., xlab=r("expression(x + alpha)"), ...).
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 

