Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove spacing around plotting area in r

Tags:

plot

r

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

like image 935
Elizabeth Avatar asked Sep 06 '12 12:09

Elizabeth


People also ask

How do I reduce margins in R plot?

You can adjust the size of the margins by specifying a margin parameter using the syntax par(mar = c(bottom, left, top, right)) , where the arguments bottom , left … are the size of the margins. The default value for mar is c(5.1, 4.1, 4.1, 2.1).

What does the par function do in R?

The par() function is used to set or query graphical parameters. We can divide the frame into the desired grid, add a margin to the plot or change the background color of the frame by using the par() function. We can use the par() function in R to create multiple plots at once.

How do I increase plotting region in R?

Use par(mai = c(bottom, left, top, right)) before the plot. It will create extra space around the plot area.

How do you identify the default margins of graph using par function gives the default margins?

par(mai=c(1.02,0.82,0.82,0.42)) The numbers used above are the default margin settings in R. You can verify this by firing up the R prompt and typing par(“mar”) or par(“mai”). You should get back a vector with the above values.


2 Answers

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

like image 63
plannapus Avatar answered Sep 20 '22 19:09

plannapus


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.

like image 37
Alan Avatar answered Sep 22 '22 19:09

Alan