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.

 

AB testing webpage CSS

AB testing is one of the most talked optimisation strategies for webpages. Google made it very popular by optimising it thoroughly in their pages. It’s using crowd-source intelligence for website optimisation…

The basic idea of AB testing is that you have 2 alternative pages that you want to test and see which of the two performs the best. The notion of performance can be which of the two alternatives led to more sells, or more clicks, or what ever you want to convert to.

Then the analysis is just a matter of computing the statistical relevance of the difference of the two alternatives (a basic two-sample proportion test, or a two-sample Z-test, for those who want the maths).

Google provides a great tool to do Website Optimisation with AB testing (and Multi-variable testing), but the problem is that you can’t really do CSS testing with it (there are some ways around it but I don’t like them). For this I decided to roll out my own AB testing for CSS alone.

It uses Javascript to apply styles to the webpage as it loads and it stores cookies that will track information for the conversion part. My basic research question for my experiments with sixhat.net is “How do CSS styles affect people website usability?”. This is a simple question with lot’s of sub questions associated with it.

Here I’m using sixhat.net as my lab. It’s simply my blog and probably you have seen some natural changes over time, if you are a regular reader (that I appreciate). But some of the answers I get from my toy website are then translated to bigger projects (for example ASSYST received some inputs from the AB testing done here.)

If you come here sometimes and find that the website really looks strange, maybe its is because I’m testing something weird. Don’t worry, each test takes little time and everything will be back to “normal” in a couple of days (usually)!

Springer crap paperback books are expensive – Rant!

<book rant>

I Love books. I buy lots of books. Just today I received a package with two books that costed me over 150€. Two paperback books. That’s a lot! But this is not a problem. I need them. I love them. I pay the price.

What pissed me off, was that this book, The Geometry of Biological Time, published by Springer, the most expensive of the two, is such a lazy print from Springer. It seems that they just used a Xerox machine and glued 800 pages to make the binding. What a crap work this is, springer? This would be acceptable for a small shop around the corner, but not from you, and not at these prices!

</book rant>

Day 4 at ETH – The Food

Zürich IV

After 4 days, I miss my diet… Here, everything is based on cheese… and cheese, and cheese… With all the options the quality is really high and we can really enjoy these plates, but I want some grilled fresh fish please…

Today we had dinner in one “picturesque” house that appears in all Zürich guides: The Rheinfelder Bierhalle !Depicted is their internationally famous Jumbo-Jumbo-Cordon Bleu (It’s longer than a 15″ laptop) eaten with the help of a Schneider-Weisse beer (Yes, that’s 1/2L and we had more than we can remember). Now I have to digest all this… Tomorrow night I’ll go back to my diet… if I can eat anything by then.

Day 3 at ETH – Lot’s of things happenning…

Some notes:

  1. Zurich today has a beautiful sun spying on us. It was fun to get its warmth during lunch…

  2. Don’t understand what is going on with my bank. Can’t get more than 100 Swiss Francs (~80€) each time I try to withdraw some money. It has to be something with the Swiss banks as I used this card in other countries and I managed to withdraw higher values… So… Swiss banks don’t really want me spending money here, and 100 CHF isn’t any REAL Money… for Swiss terms…

  3. We had today a kind of team building social game that was very interesting and went then with the team to drink some beers in a tapas bar. We then moved on to a (I presume) typical folk music bar from Switzerland (you can see a short video above). They told me that this was the first time that they had this type of event inside the team, and I think that by the success of it they will repeat it again.

  4. Finally … It’s still cold… Argh… but worse than the cold is the wall you hit when you move from 1 room at 22ºC to the outside at 2ºC. It’s something I won’t get used, ever…

  5. Science/geek stuff… yesterday I was here (If you know what this is, I’ll give you a Swiss cheese), and today I was briefed a bit about Brutus! LOL, Fun times…

Day 2 at ETH – Papers brainstorming and 2 talks

Zurich II

Today at this short ETH visit we spent most of the day discussing ongoing projects and brainstorming ideas and possible future papers. We also had the pleasure to attend 2 talks in the late afternoon that were very interesting. The first one was by David Rand from Harvard about Reward, Punishment, and the evolution of cooperation. The second was by Ole Peters from Imperial College, about the Role of time in economics. This is was part of ETH Interdisciplinary Seminar FS 2011. Next week it theobservatorium.eu turn to be there on stage…

On another front… Can anyone in a place that is warmer than freezing point send me some heat? I’m cold!

Off to ETH, Zurich…

I’m almost off to Zurich for a week of scientific debate and brainstorm at ETH. I’ll have a presentation as soon as I get off the plane, where I’ll be showing mainly the work I’ve been doing on the http://theobservatorium.eu/ . This will be my first time in Zurich and for what I see the weather is not even close to Lisbon… :( Temperature tops at 4ºC while we are here it is around 20ºC… so I imagine that I wont go out much, but I’ll try… Now… I just need ideas of things to visit: Can you help?