R中绘图中的连接点

connecting points in plot in R

这是一个简单的任务,但由于某种原因,它没有给我想要的输出。

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

我想绘制 x1,y1 并用一条线连接这些点,但这条线不是 显示。 我使用坐标轴是因为我希望显示这些特定标签。

有什么推荐吗?

您还必须提供 x 坐标才能绘图。 您还必须修改轴函数中的 at 参数。

   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(xx1,y1, col = "gray")

请注意,您可以指定 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")

一次获取线和点。

只需在绘图函数参数中添加“type = l”(l 代表行)