Search code examples
rplot

Plot random numbers on y, x is the range


I want to generate 50 random values in the range 0-180. What I did:

a=runif(50,min=0,max=180)

Now I want this plot: x axis is the range from 0-180, y axis is just a single value (1), showing a dot where the 0-180 number exists in the "a" set, nothing if it doesn't.

Do I need to generate numbers 0-180 for the x axis, or can R do it with only the "a" set? If I do plot(a), I get a scatter plot where x is the index and y is the value.

img


Solution

  • If you want something resembling the example closely, here it is:

    x <- runif(50, 0, 180)
    
    # make an empty plot
    plot.new()
    plot.window(xlim = c(0,185), ylim = c(0,2), xaxs = "i")
    
    # add grid lines
    abline(h = 1, col = "gray", lwd = 2)
    abline(v = c(0,90,180), col = "gray", lwd = 2)
    
    # add points
    points(x, rep(1, length(x)), pch = 19, cex = 1.5)
    
    # add axes
    axis(1, at = c(0,90,180), font = 2, cex.axis = 2, tck = 0)
    box(bty = "L", lwd = 3)
    

    result