Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a ridiculously wide plot

Tags:

plot

r

I have a long time series of 10000 observations that I want to visualize. The problem is, if I just plot it normally the time-dimension will be squished and none of the fine detail of the time-series that I want to visualize will be apparent. For example:

 plot((sin(1:10000/100)+rnorm(10000)/5),type='l')

What I would like is to somehow plot the following together side by side in one gigantically long plot without using par(mfrow=c(1,100)). I then want to export this very wide plot and simply scroll across to vizualise the whole series.

 plot((sin(1:10000/100)+rnorm(10000)/5)[1:100],type='l')
 plot((sin(1:10000/100)+rnorm(10000)/5)[101:200],type='l')
 plot((sin(1:10000/100)+rnorm(10000)/5)[201:300],type='l')
 .....

Eventually I would like to have 3 or 4 of these gigantically wide plots on top of each other with a par(mfrow=c(4,1)).

I know that the answer has something to do with the pin setting in par, but I keep getting Error in plot.new() : plot region too large. I'm guessing this has something to do with the interaction of pin with the other par parameters

Bonus points are awarded if we can get the pixel height and width exactly right. It is preferable that the plot doesn't skip random pixels due to the export sizing being imperfect.

Further bonus points if the image can be encoded in a .html. and viewed this way

like image 646
user2763361 Avatar asked Oct 03 '13 05:10

user2763361


3 Answers

An alternative that you might consider is svg, which will produce something of better quality than png/jpeg in any case.

Something like

svg(width = 2000, height = 7)
par(mfrow=c(4,1), mar = c(4, 4, 0, 2))
for (i in 1:4){
    plot((sin(1:10000/100)+rnorm(10000)/5),type='l', 
         bty = "l", xaxs = "i")
}
dev.off()

will produce a very wide plot, just over 1MB in size, which renders quite nicely in Chrome.

Note the width and height are in inches here.

P.S. svg also offers the potential for interactive graphics. Just seen a nice example allowing the user to select a region of a long time series to zoom in on, see Figure 22 in Dynamic and Interactive R Graphics for the Web: The gridSVG Package, a draft paper by Paul Murrell and Simon Potter.

like image 76
Heather Turner Avatar answered Oct 19 '22 09:10

Heather Turner


It could be a Cairo-specific problem, or it could be a lack of RAM on your machine. The following code works fine for me on a Windows 7 machine with 8GB RAM.

png("wide.png", width = 1e5, height = 500)
plot((sin(1:10000/100)+rnorm(10000)/5),type='l')
dev.off()

If I change the width to 1e6 pixels, then R successfully creates the file (it took about a minute), but no image viewing software that I have available can display an image that large.

like image 32
Richie Cotton Avatar answered Oct 19 '22 09:10

Richie Cotton


I would go on some alternative route. First of all, what exactly is the point of viewing the entire plot at hi-res? If you're searching for some sort of anomalies or irregularities, well, that's what data processing is for :-) . Think about something like finding allx > 3sigma, or doing an FFT, etc.

Next, if you really want to examine the whole thing by eye, how about writing some R-TclTK code or using dynamicGraph or iplots or zoom to produce an interactive graph that you can scroll thru "live."

ETA: IIRC RStudio has tools for interactive graph scrolling and zoom as well.

like image 1
Carl Witthoft Avatar answered Oct 19 '22 07:10

Carl Witthoft