【问题标题】:Positioning axes labels定位轴标签
【发布时间】:2018-12-21 17:20:15
【问题描述】:

如何在下图中将 y 轴标签从绘图区域的左侧移动到右侧,并将 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,3),c(1,3),type="n",main="title",xlab="xlab-move me above plot",ylab="ylab-move me      right of plot",axes=F,asp=1)
axis(1,pos=1)
axis(2,pos=1)


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

【问题讨论】:

  • 顺便说一句,这个问题正是my comment你上一个问题的原因。
  • 我还是不明白你说的plannapus是什么意思。更改坐标轴而不更改绘图区域如何导致 x 和 y 标签出现问题?
  • 您只是移动了轴,而不是图的限制:您的标签与图的限制而不是轴的距离为给定距离(在您的 axis 调用后键入 box(),使情节的界限出现,你会明白我的意思)。不过,这不是什么大问题,因为 Backlin 的解决方案仍然可以正常工作。
  • 啊,我明白你的意思了。谢谢你的课:)

标签: r plot axes


【解决方案1】:

在绘图的右侧和顶部绘制轴

默认情况下,R 将绘制绘图区域下方的 x 轴和左侧的 y 轴。您可以通过以下方式更改此行为:

plot(1:100, cumsum(rnorm(100)), type="l", axes=FALSE) # Do not plot any axes
axis(3)   # Draw the x-axis above the plot area
axis(4)   # Draw the y-axis to the right of the plot area
box()

还可以移动您设置的标签ann=FALSExlab="", ylab="",然后使用mtext 添加它们,其中side=1 是底部,2 是左侧,3 是顶部,4 是右侧。 line 控制与绘图区域的距离。

plot(1:100, cumsum(rnorm(100)), type="l", axes=FALSE, ann=FALSE)
axis(3)
box()
mtext("Top axis", side=3, line=3)

改变标签、刻度和绘图区域之间的距离。

在调用plot之前,使用mgp参数来控制这些细节,像这样

par(mgp=c(axis.title.position, axis.label.position, axis.line.position))

或在plot 命令本身中,像这样

plot(1:100, cumsum(rnorm(100)), type="l", mgp=c(2,1,.5), las=1)

还请注意las 参数,该参数将所有刻度标签水平转换,这使它们更易于阅读。

【讨论】:

  • 塔卡塔卡!虽然我不得不使用 line=-1 让它改变方向,但效果很好。是否可以以类似的方式“翻转”轴刻度和相关编号?如果您觉得这是一个不同的问题,我可以发布一个新问题。再次感谢
  • las 参数控制刻度标签、mtext 和其他各种文本相关内容的旋转。使用plot(..., las=1) 使所有刻度标签水平。这确实是一个非常好的主意,它使图表更易于阅读。
  • 嗯...对不起,我认为我的问题可能不清楚。我想将刻度线和数字移动到绘图区域的上方和右侧。 las 命令仅用于相对于轴旋转文本。我想将轴移动到绘图的相反侧。
  • 哦,我不知道。甚至从未见过这样的 R 图。
猜你喜欢
  • 2011-01-30
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多