How to specify the actual x axis values to plot as x axis ticks in R

后端 未结 4 1920
太阳男子
太阳男子 2020-11-28 03:00

I am creating a plot in R and I dont like the x axis values being plotted by R.

For example:

x <- seq(10,200,10)
y <- runif(x)

plot(x,y)
         


        
4条回答
  •  一生所求
    2020-11-28 03:24

    You'll find the answer to your question in the help page for ?axis.

    Here is one of the help page examples, modified with your data:

    Option 1: use xaxp to define the axis labels

    plot(x,y, xaxt="n")
    axis(1, xaxp=c(10, 200, 19), las=2)
    

    Option 2: Use at and seq() to define the labels:

    plot(x,y, xaxt="n")
    axis(1, at = seq(10, 200, by = 10), las=2)
    

    Both these options yield the same graphic:

    enter image description here


    PS. Since you have a large number of labels, you'll have to use additional arguments to get the text to fit in the plot. I use las to rotate the labels.

提交回复
热议问题