R in the Top 20 of Programming Languages

Programming languages come and go, but its nice to see what’s gaining momentum and what’s not. In the latest Tiobe report for January 2012 we can see some interesting surprises in the top 20 chart of programming languages. C is still highly demanded and closing on Java. Both account for 1/3 of the programming languages panorama.

Other interesting aspect is the fading of Python. Python lost half of it’s market share. Maybe this is because of Python 3 and the incompatibilities with Python 2.x that might have sent many programmers in search other solutions. Another problem might be GIL that hinders thread programming in Python in a time when programming is moving to the concurrent and distributed programming.

Also interesting is the rise of R. R is one of my favorite languages for science. It makes reproducibility of research results very easy (specially if you use Sweave with R) and for any kind of statistical analysis it is almost perfect. It also produces great plots for scientific publications.

How to plot multiple data series in R?

plot multiple data series - Multiple plots in R

I usually use ggplot2 to plot multiple data series, but if I don’t use ggplot2, there are TWO simple ways to plot multiple data series in R. I’ll go over both today.

Matlab users can easily plot multiple data series in the same figure. They use hold on and plot the data series as usual. Every data series goes into the same plot until they use hold off.

But can the same thing be done in R? R is getting big as a programming language so plotting multiple data series in R should be trivial.

The R points and lines way

Solution 1: just plot one data series and then use the points or lines commands to plot the other data series in the same figure, creating the multiple data series plot:

> plot(time, series1, type='l', xlab='t /s', ylab='s1')
> points(time, series2, type='l')

Plot Multiple Data Series the Matlab way

Solution 2: this one mimics Matlab hold on/off behaviour. It uses the new parameter of graphical devices. Let’s see how:

Setting new to TRUE tells R NOT to clean the previous frame before drawing the new one. It’s a bit counter intuitive but R is saying “Hey, theres a new plot for the same figure so don’t erase whatever is there before plotting the new data series“.

Example (plot series2 on the same plot as series1):

> plot(time, series1, type='l', xlim=c(0.0,20.0), 
+ ylim=c(0.0,1.0), xlab='t /s', ylab='s1')
> par(new=T)
> plot(time, series2, type='l', xlim=c(0.0,20.0), 
+ ylim=c(0.0,1.0), xlab='', ylab='', axes=F)
> par(new=F)

The par(new=T) tells R to make the second plot without cleaning the first. Two things to consider though: in the second set axes to FALSE, and xlabel and ylabel to empty strings or in the final result you’ll see some overlapping and bleeding of the several labels and axes.

Finally, because of all this superimposing you need to know your axes ranges and set them up equally in all plot commands (xlim, and ylim in this example are set to the range [0,20] and [0,1]).

R doesn’t automatically adjust the axes, as it doesn’t use the first frame as reference or the multiple data series. You need to supply these values or you’ll end up with a wrong looking plot like Marge Simpson’s hair.

In conclusion, either solution will work to plot multiple data series inside R, but sometimes one will be better than the other. Sometimes your data series represent different properties and you’ll need to specify the y ranges individually. In this case the latter option might be useful. Other times you just want a quick exploratory data analysis plot, or your data series are measuring the same property and the former method suffices.

 

R versus Matlab in…

R versus Matlab in <put your domain here> has been a long discussion. I end up using both, but I find that I use Matlab for more simple things and R for things where I want the highest quality in figure output or where the extra mile of not having a fine polished IDE is compensated in the end by the results.

I agree that the argument of R having a stranger IDE is annoying, but developing one might require some extra time… but as in Open source I think that you must iterate, and often, and R as become very good, stable and powerful. Many are flocking to R even without ever starting with Matlab. That’s good.

via R versus Matlab in Mathematical Psychology.