34

When I create the following plot I get unwanted space between the plotting area and the axis (i.e. the white space between the blue box and the x axis. How can I remove this space and make the plotting area flush with the plot axes? Thanks.

xleft<-c(1,2,2.5)
xright<-c(2,2.5,2.75)
ybottom<-c(1,2,2.5)
ytop<-c(2,2.5,2.75)

par(mar = c(15,15,2.75,2.75) + 0.1)
plot(c(1,2.75),c(1,2.75),type="n",main="title",xlab="site.x",ylab="ylab")
rect(xleft,ybottom,xright,ytop,col=c("blue","red","green"))

#Label position along  axes
x.label.position<-(xleft+xright)/2
y.label.position<-(ybottom+ytop)/2

 #Labels
 x.label<-c("Long species Name1","Long species Name2","Long species Name3")
 y.label<-c("Long species Name4","Long species Name5","Long species Name5")

 text(par()$usr[1]-0.5,y.label.position,y.label,xpd=TRUE,adj=1)
 text(y=par()$usr[3]-0.5,x=x.label.position,x.label,xpd=TRUE,adj=1,srt=90)

 par(xpd=TRUE)
 legend(-0.1,0,legend=c("Species A","Species B","Species C"),fill=c("blue", "red", "green"))

enter image description here

UPDATE I tried the suggestion from plannapus with my actual data but can only get the y-axis to behave is there some else in this chunk of code that is adding space to the sides of the plotting area?

quartz("colour.plot")
par(mar=c(15,15,4,2)+0.1)#sets margins of plotting area

#create the data plot
    plot(c(0,100), c(0,100), type = "n", main = paste(x,"vs",y," -",depth),xlab=paste("Species composition in remainder ",x),ylab=paste("Species composition in remainder ",y),asp=1,xaxs="i",yaxs="i")

#Add the rectangles
rect(mdf$xleft,mdf$ybottom,mdf$xright,mdf$ytop,col=mdf$colour)

produces

enter image description here

Elizabeth
  • 6,391
  • 17
  • 62
  • 90
  • 1
    "is there some else in this chunk of code that is adding space to the sides of the plotting area?" Yes: `asp=1`. With this you're forcing the x and y axes to be equal, which probably conflict with the size of your plot region. – plannapus Sep 06 '12 at 13:12
  • adding `par(pty="s")` before the call to `plot` should force the plot region to be square (hence `"s"`) and not rectangular as on your image and therefore correct for the `asp` "glitch". – plannapus Sep 06 '12 at 13:20
  • I think you should go for @plannapus answer, or simly set the `xlim`, `ylim` to match the region you want to plot. Although @Alan's solution gives the right appearance it is not conceptualy correct for what you want to do here. It only "cures the symptom" so to say. – Backlin Sep 06 '12 at 15:14

2 Answers2

43

There is an argument in function plot that handles that: xaxs (and yaxs for the y-axis). As default it is set to xaxs="r" meaning that 4% of the axis value is left on each side. To set this to 0: xaxs="i". See the xaxs section in ?par for more information.

plot(c(1,2.75),c(1,2.75),type="n",main="title",xlab="site.x",ylab="ylab", xaxs="i", yaxs="i")
rect(xleft,ybottom,xright,ytop,col=c("blue","red","green"))

enter image description here

plannapus
  • 18,529
  • 4
  • 72
  • 94
  • @planapus I would like to use your solution but I having trouble because I am using asp=1 which adds space to both sides of the x-axis with you solution (see example under update in question). Is there anyway to avoid this problem? – Elizabeth Sep 06 '12 at 15:30
  • @Elizabeth Did you try `par(pty="s")` as I suggested in the comments above? (Ideally though the solution would be to define exactly the size of your plotting region but that is a pain to do) – plannapus Sep 06 '12 at 15:31
  • I did but just noticed I had an error in that line. Fixed and now it works. Thanks :) – Elizabeth Sep 06 '12 at 15:34
  • Cool feature! Didn't know about `axis(..., pos)` before, but since it plots the axis inside the plot area rather than adjust it I suspect [this is what it is intended for](http://www.ats.ucla.edu/stat/r/library/build2r.png). – Backlin Sep 06 '12 at 15:37
5
plot(c(1,2.75),c(1,2.75),type="n",main="title",xlab="site.x",ylab="ylab",axes=F) # ann
axis(1,pos=1)
axis(2,pos=1)

All axes are remove then you can add new axes to the pos you want.

Alan
  • 3,153
  • 2
  • 15
  • 11
  • It does work correctly but it should be noted that this solution move the axes but does not modify the plotting area. – plannapus Sep 06 '12 at 13:28