軸ラベルの配置 質問する

軸ラベルの配置 質問する

次のグラフで、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"))

ここに画像の説明を入力してください

ベストアンサー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=FALSE、 またはを設定しxlab="", ylab=""、その後 で追加しますmtext。ここで、side=1は下、 1 は​​左、 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)

ここに画像の説明を入力してください

ラベル、目盛り、プロット領域間の距離を変更します。

mgpこれらの詳細を制御するには、 を呼び出す前にplot次のように パラメータを使用します。

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すべての目盛りラベルを水平にして読みやすくするパラメータにも注意してください。

おすすめ記事