删除r中绘图区域周围的间距 [英] Remove spacing around plotting area in r

查看:121
本文介绍了删除r中绘图区域周围的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建以下绘图时,在绘图区域和轴之间得到了多余的空间(即,蓝色框和x轴之间的白色空间.如何删除该空间并使绘图区域与绘图轴齐平?谢谢.

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"))

更新 我用实际数据尝试了Plannapus的建议,但只能使y轴起作用,这部分代码中是否还有其他内容在绘图区域的两侧增加了空间?

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)

产生

推荐答案

函数plot中有一个参数可以处理以下内容:xaxs(y轴为yaxs). 默认情况下将其设置为xaxs="r",这意味着在每一侧都保留了4%的轴值.要将其设置为0:xaxs="i".有关更多信息,请参见?par中的xaxs部分.

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"))

这篇关于删除r中绘图区域周围的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆