Search code examples
rplotscatter-plot

connecting points in plot in R


this is a simple task but for some reason it is not giving me the desired output.

x1 = c(100000,250000,500000,750000,1000000)
y1 = c(1.076904,3.917412,12.365130,23.084268,37.234246)
plot(y1, pch=5, ylim=c(1,50),xlab="Sample Size", ylab="Time(s)" , main = "Time relative to Sample Size-NonGreedy", xaxt = "n")
axis(1, at=1:5, labels = x1)
lines(x1,y1, col = "gray")

I want to plot x1,y1 and connect the points with a line, but the line is not showing. I am using axes because I want these specific labels to show.

Any recommendations?


Solution

  • You have to give the x-coordinates to plot as well. Also you have to modify the at argument in your axis function.

       x1 = c(100000,250000,500000,750000,1000000)
        y1 = c(1.076904,3.917412,12.365130,23.084268,37.234246)
        plot(x1, y1, pch=5, ylim=c(1,50),xlab="Sample Size", ylab="Time(s)" , main = "Time relative to Sample Size-NonGreedy", xaxt = "n")
        axis(1, at = x1, labels = x1)
        lines(x1, y1, col = "gray")
    

    Note that you can specify type = "b"

    plot(x1, y1, pch = 5, ylim=c(1,50),xlab="Sample Size", ylab="Time(s)" , 
         main = "Time relative to Sample Size-NonGreedy", xaxt = "n", type = "b")
    

    to get lines and points at once.