R 定位轴标签

R 定位轴标签,r,plot,axes,R,Plot,Axes,在下图中,如何将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",y

在下图中,如何将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"))
xleft打印轴位于打印的右侧和顶部
默认情况下,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()

还要移动设置的标签,请使用
mtext
,其中
side=1
为底部,2为左侧,3为顶部,4为右侧<代码>直线
控制与绘图区域的距离

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(1:100, cumsum(rnorm(100)), type="l", mgp=c(2,1,.5), las=1)
或者在
plot
命令本身中,如下所示

par(mgp=c(axis.title.position, axis.label.position, axis.line.position))
plot(1:100, cumsum(rnorm(100)), type="l", mgp=c(2,1,.5), las=1)


还要注意
las
参数,该参数使所有刻度标签水平对齐,这使它们更易于阅读。

顺便说一句,这个问题正是你上一个问题的原因。我仍然不明白你的意思。如何更改轴而不是打印区域会导致x和y标签出现问题?您只是移动了轴,而不是打印的限制:您的标签与打印的限制之间的距离是给定的,而不是与轴之间的距离(键入
box()
在调用
axis
后,显示绘图的限制,您将了解我的意思)。这不是一个大问题,但是因为贝克林的解决方案使它仍然有效。AhI明白你的意思不。谢谢你的教训:)塔卡塔卡!虽然我不得不使用line=-1来让它改变方向,但效果很好。是否可以以类似的方式“翻转”轴标记和相关编号?如果你觉得这是一个不同的问题,我可以发布一个新问题。再次感谢
las
参数控制勾号标签的旋转、
mtext
和其他各种与文本相关的内容。使用
plot(…,las=1)
使所有刻度标签水平。这确实是一个非常好的主意,它使图表更容易阅读。嗯……对不起,我想我的问题可能不清楚。我想将记号和数字移动到绘图区域的上方和右侧。las命令只用于相对于轴旋转文本。我想将轴移动到绘图的对立面。哦,我不知道。我从没见过这样的情节。