Positioning axes labels

断了今生、忘了曾经 提交于 2019-11-27 11:41:36

Ploting axes on the right and top sides of a plot

By default R will plot the x-axis below the plot area and the y-axis to the left of it. You can change this behaviour in this way:

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

To also move the labels you set ann=FALSE or xlab="", ylab="" and add them afterwards with mtext, where side=1 is bottom, 2 is left, 3 is top, 4 is right. line controls the distance from the plot area.

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

Changing distance between labels, ticks and plot area.

Use the mgp parameter to control these details, either before the call to plot, like this

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

or in the plot command itself, like this

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

Also note the las parameter that turns all tick labels horisontal, which makes them easier to read.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!