[R] adding a fram around R plot

Jim Lemon jim at bitwrit.com.au
Tue Dec 31 05:07:04 CET 2013


On 12/31/2013 02:21 AM, Tohamy Yousef wrote:
> Hi all,
>
>   I need to know how to put a closed frame around my plot.  I am
> plotting
> using the field package, and I have been able to use box() with
> limited
>   success. Box() puts a border around only a part of  the
> plot area not all.
> I saw that there was a similar problem in this group but I used the suggested solution, par(c('bty','xpd')),
> but without success.
>
> How can I add a frame around my plot

Hi Tohamy,
Usually a box around the entire figure is wanted when embedding the plot 
in a document, and this is handled by the page formatting application. 
However, if you really need to add this:

outerBox<-function(lty=1,lwd=1,border=NULL) {
  plotlim<-par("usr")
  marg<-par("mar")
  ppin<-par("pin")
  pfin<-par("fin")
  xin<-(plotlim[2]-plotlim[1])*0.003
  yin<-(plotlim[4]-plotlim[3])*0.003
  xleft<-plotlim[1]-(plotlim[2]-plotlim[1])*
   ((pfin[1]-ppin[1])/ppin[1])*(marg[2]/(marg[2]+marg[4]))
  xright<-plotlim[2]+(plotlim[2]-plotlim[1])*
   ((pfin[1]-ppin[1])/ppin[1])*(marg[4]/(marg[2]+marg[4]))
  ybottom<-plotlim[3]-(plotlim[4]-plotlim[3])*
   ((pfin[2]-ppin[2])/ppin[2])*(marg[1]/(marg[1]+marg[3]))
  ytop<-plotlim[4]+(plotlim[4]-plotlim[3])*
   ((pfin[2]-ppin[2])/ppin[2])*(marg[3]/(marg[1]+marg[3]))
  cat(plotlim,"\n",xleft,ybottom,xright,ytop,"\n")
  par(xpd=TRUE)
 
rect(xleft+xin,ybottom+yin,xright-xin,ytop-yin,lty=lty,lwd=lwd,border=border)
  par(xpd=FALSE)
}

Jim



More information about the R-help mailing list