Search code examples
rsequential

R - xlim sequential increase


Is it possible to increase the values in the X-axis with 1? For example - 1,2,3,4,5 etc.

Right now I use this:

xlim=c(1,16)

And the result is:

enter image description here

Which doesn't look nice, the ideal would be to have a sequential increase with 1 - from 1 to 16, since I have 16 values for the X-axis.


Solution

  • xlim can be finely controlled with axis. To make it clear, I will reproduce one plot without axis control and one instead where we performed a modification on the scale.

    x <- rnorm(100, 10, 2)
    y <- rnorm(100, 10, 2)
    
    par(mfrow = c(1, 2))
    

    Plot 1 is produced without axis control

    plot(x, y, main = "Plot 1")
    

    In Plot 2 we set a demonstrative xlim and ylim that produce a scale from 0 to 20 for both axes. We can more finely tune it with axis: to make an example, I create a scale by 1 for axis x and by 5 for axis y

    plot(x, y, xlim = c(0, 20), ylim = c(0, 20), main = "Plot 2")
    axis(1, at=seq(0, 20, 1))
    axis(2, at=seq(0, 20, 5))
    

    enter image description here

    That's not all. axis allow a really fine work on your plot axis with its arguments.

    axis(side, at=, labels=, pos=, lty=, col=, las=, tck=, ...)
    

    side

    an integer indicating the side of the graph to draw the axis (1=bottom, 2=left, 3=top, 4=right)

    at

    a numeric vector indicating where tic marks should be drawn

    labels

    a character vector of labels to be placed at the tickmarks (if NULL, the at values will be used)

    pos the coordinate at which the axis line is to be drawn. (i.e., the value on the other axis where it crosses)

    lty

    line type

    col

    the line and tick mark color

    las

    labels are parallel (=0) or perpendicular(=2) to axis

    tck

    length of tick mark as fraction of plotting region (negative number is outside graph, positive number is inside, 0 suppresses ticks, 1 creates gridlines) default is -0.01