0

I can't seem to find much information on this. I would like to rotate the main plot in and image while leaving the legend and title unaltered.

Here's an example. You'll need fields package which gives you the image.plot function.

x=1:10
y=1:10
z=matrix(-50:49,10,10)
image.plot(x,y,z, main="Some Fancy Title",yaxt="n",xaxt="n", ylab="", xlab="")

So again I just want to rotate the main image and leave the title and legend in place. Btw I'm rotating 36 degrees so I can't just rotate the matrix I'm feeding image.plot. The grid package seems to lead me in the right direction but it is frustrating how I cannot plot directly into their so called viewports with plot or anything else from base. I must say not a fan of the grid package so far.

I found this post which provides some helpful information in answer 5. I know from this that I should be able to use grid.cap() to capture a raster of my device and use grid.raster(cap, vp=viewport(angle=36)) to import the plot into the correct viewport. I'm having problems getting this method to working for me and it seems pretty inefficient to boot. I will be plotting literally thousands of images to create gifs of various temporally and spatially varying data I have modeled.

What I'm looking for is either a way to get grid.cap() to work with the above example and rotate only the main part of the image 36 degrees or an alternative doing the same thing that will work well with my volume of plots. Much thanks to anyone that gives this some thought.

Community
  • 1
  • 1
CCurtis
  • 1,770
  • 3
  • 15
  • 25

1 Answers1

1

The issue is that image.plot is not one plot but two: the main one and the legend. If you want to use package fields' image.plot function, you'll have to fiddle with the function's code directly. Otherwise you can use function image from the base package graphics and add manually your legend and your title.

Here's one way to do it:

library(grid)
x=1:10
y=1:10
z=matrix(-50:49,10,10)

layout(matrix(c(1,2),ncol=2), widths=c(2,1))         
par(mar=c(5,3,5,3))
image(x,y,z,yaxt="n",xaxt="n", ylab="", xlab="",col=heat.colors(50)) 
cap <- grid.cap()
grid.newpage()
grid.raster(cap, x=unit(0.6,'npc'), #You can modify that if the plot 
            y=unit(0.5,'npc'),      #ends up outside the figure area
            vp=viewport(angle=36))
mtext("Some fancy title",side=3,cex=1.5,line=2) #Plot your title
par(mar=c(5,8,5,3))
plot(NA,ax=F,ann=F,type="n",xlim=c(0,1),ylim=c(0,50),yaxs="i")
for(i in 1:50)rect(0,i-1,1,i,col=heat.colors(50)[i],border=NA)
box()
axis(4,las=2,at=seq(0,50,by=10),labels=seq(-50,50,by=20))

enter image description here

plannapus
  • 18,529
  • 4
  • 72
  • 94
  • 1
    Thanks for this but I'm not able to get your output. In your code after calling `grid.raster(cap,...` I get the warning `Error in UseMethod("as.raster") : no applicable method for 'as.raster' applied to an object of class "NULL"`. I have a pretty fresh instal of R and have already tried this on another machine. Plotting on x11() doesn't produce an error but only plots the legend and title. Are you leaving anything out here? Any idea why this is not working for me? Trying the example from the post I mention also produces this error. – CCurtis Dec 19 '13 at 20:20
  • Also you may not be aware but with `image plot` you can set `legend.only=TRUE` and it will only plot the legend where it normally appears however you will still need to add the title separately. – CCurtis Dec 19 '13 at 20:25
  • I didn't let anything out, and the example from the linked post works for me so i don't really know what to say right now. I'll try to investigate on what can produce your error. COncerning `image.plot` if you look at the function code you'll see that not only does it create two plot but it reinitialize the plotting region at the end so i don't think it's possible to grab the plot with those `grid` functions without modifying significantly the function's code. – plannapus Dec 20 '13 at 07:49
  • I just tried the code in a vanilla R session and it works as well without error. – plannapus Dec 20 '13 at 07:54
  • 1
    Are you by any chance using Rstudio instead of the R GUI or the command line? – plannapus Dec 20 '13 at 08:12
  • Yes I'm using Rstudio. I'll give the GUI and command line a try. – CCurtis Dec 20 '13 at 08:19
  • 1
    Ok for whatever reason command line works for me but R GUI doesn't. No idea why this would happen but I run everything from script so I can work with it. Sad to hear what you found in `image.plot`. Would have been nice to plot right into a viewport. Anyhow thanks for all your hard work and here's a well deserved upvote :D – CCurtis Dec 20 '13 at 08:53