[R] Plotting color.legend() outside of plot region

Tim Clark mudiver1200 at yahoo.com
Mon Nov 30 22:12:08 CET 2009


Dear List,

I am trying to plot a color.legend() in the right outer margin of my device region.  I have read multiple threads on the subject and still can't get it right.  I have stolen an example from one of the threads to demonstrate my problem.  I have extended the outer margin using par(oma()), and have used par(xpd=NA) to tell it to plot in the device region.  I can get the legend to plot in the figure region but it will not plot in the outer margin.  What am I doing wrong?  Example follows with the legend in the figure region.  I would like it more to the right in the device region. 

Thank,

Tim

library(TeachingDemos)
op <- par(mfrow = c(3,3), ## split region
          oma = c(0,0,4,12) + 0.1, ## create outer margin
          mar = c(5,4,2,2) + 0.1) ## shrink some margins
plot(1:10, main = "a", pch = 1:2, col= 1:2)
plot(1:10, main = "b", pch = 1:2, col= 1:2)
tmp1 <- cnvrt.coords( 0.5, 0, input='plt' )$tdev # save location for mtext
plot(1:10, main = "c", pch = 1:2, col= 1:2)
plot(1:10, main = "d", pch = 1:2, col= 1:2)
plot(1:10, main = "e", pch = 1:2, col= 1:2)
plot(1:10, main = "f", pch = 1:2, col= 1:2)
plot(1:10, main = "g", pch = 1:2, col= 1:2)
plot(1:10, main = "h", pch = 1:2, col= 1:2)
plot(1:10, main = "i", pch = 1:2, col= 1:2)
## title
mtext("My Plots", side = 3, outer = TRUE, font = 2, line = 1, cex = 1.2,at=tmp1$x)


## draw legend
#Set colors
  Satelite.Palette <- colorRampPalette(c("blue3","cyan","aquamarine","yellow","orange","red"))
  mycol<-Satelite.Palette(101)
  
#Add legend
  BL<-unlist(corner.label(x=-1,y=-1,figcorner=FALSE))    #Coordinates for bottom left corner of plot
  TR<-unlist(corner.label(x=1,y=1,figcorner=FALSE))      #Coordinates for top right corner of plot

  par(xpd=NA) #Plots to the device region (outer margin)
  library(plotrix)
  col.labels<-seq(0,100,20)
  color.legend(TR[1],BL[2],TR[1]+1,TR[2],col.labels,mycol,align="rb",gradient="y")

#Reset par
  par(op)





Tim Clark
Department of Zoology 
University of Hawaii




More information about the R-help mailing list