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

Peter Ehlers ehlers at ucalgary.ca
Mon Nov 30 23:28:03 CET 2009


Tim Clark wrote:
> 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)
> 
The par(xpd=...) only applies to the last plot. I can't
think offhand how to avoid that. I would use Paul Murrell's
handy layout() function. Something like this (you'll have
to adjust margins, etc)

m <- matrix(c(1:9,10,10,10), 3, 4)
nf <- layout(m)
layout.show(nf)

plot(1:10, main = "a", pch = 1:2, col= 1:2)
plot(1:10, main = "b", pch = 1:2, col= 1:2)
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)

plot(0:1, 0:1, type="n", axes=F, xlab="", ylab="")
color.legend(.2,.2,.8,.8, col.labels, mycol, align="rb", gradient="y")

See ?layout

  -Peter Ehlers

> 
> 
> 
> 
> Tim Clark
> Department of Zoology 
> University of Hawaii
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
>




More information about the R-help mailing list