删除r中绘图区域周围的间距

时间:2012-09-06 12:55:37

标签: r plot

当我创建下面的绘图时,我在绘图区域和轴之间得到了不需要的空间(即蓝色框和x轴之间的空白区域。如何删除此空间并使绘图区域与绘图轴齐平谢谢。

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 我尝试使用plannapus中的建议和我的实际数据,但只能得到y轴表现在这块代码中还有其他一些东西给绘图区域的边增加了空间吗?

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)

产生

enter image description here

2 个答案:

答案 0 :(得分:30)

函数plot中有一个参数可以处理:xaxs(和yaxs表示y轴)。 默认情况下,它设置为xaxs="r",意味着每侧都有4%的轴值。要将其设置为0:xaxs="i"。有关详细信息,请参阅xaxs中的?par部分。

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

答案 1 :(得分:4)

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)

删除所有轴,然后您可以将新轴添加到所需的pos