Positioning axes labels

前端 未结 1 1647
暖寄归人
暖寄归人 2020-12-02 16:01

How can I move the y axis label from the left to the right of the plot area and the x-axis label from below to above the plot area in the following graph? Thanks

<         


        
相关标签:
1条回答
  • 2020-12-02 16:19

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

    enter image description here

    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)
    

    enter image description here

    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)
    

    enter image description here

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

    0 讨论(0)
提交回复
热议问题