如何将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"))
Run Code Online (Sandbox Code Playgroud)

Bac*_*lin 62
默认情况下,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()
Run Code Online (Sandbox Code Playgroud)

要同时移动您设置的标签ann=FALSE或xlab="", ylab=""之后添加标签mtext,其中side=1底部为2,左侧为3,顶部为4,右侧为4.line控制绘图区域的距离.
plot(1:100, cumsum(rnorm(100)), type="l", axes=FALSE, ann=FALSE)
axis(3)
box()
mtext("Top axis", side=3, line=3)
Run Code Online (Sandbox Code Playgroud)

mgp在调用之前使用参数来控制这些细节plot,就像这样
par(mgp=c(axis.title.position, axis.label.position, axis.line.position))
Run Code Online (Sandbox Code Playgroud)
或者在plot命令本身中,像这样
plot(1:100, cumsum(rnorm(100)), type="l", mgp=c(2,1,.5), las=1)
Run Code Online (Sandbox Code Playgroud)

另请注意las将所有刻度标签变为水平的参数,这使得它们更易于阅读.