lines()函数未连接图中的实际点

时间:2018-02-28 06:03:38

标签: r graph visualization

我一直在练习图表以及如何使用以下代码在R中绘图

theta = 1:100
x = sin(theta)
y = cos(theta)
op = par(bg = 'white', mar = rep(1, 4))
plot.new()
plot(x,y)
plot.window(xlim = c(-1, 1), ylim = c(-1, 1))
lines(x, y, col = hsv(0.95, 1, 1))

获取以下输出 enter image description here

现在我想跟踪线条的确切连接方式以形成此模式,因此使用了以下代码。

theta = 1:3
x = sin(theta)
y = cos(theta)
op = par(bg = 'white', mar = rep(1, 4))
plot(x,y, xlab = "Sin", ylab = "Cos", type = "p")
plot.window(xlim = c(-1, 1), ylim = c(-1, 1))
lines(x, y, col = hsv(0.95, 1, 1))

我得到以下输出 enter image description here

线条不应该连接点吗?我使用此代码

得到线条连接点的输出
theta = 1:3
x = sin(theta)
y = cos(theta)
op = par(bg = 'white', mar = rep(1, 4))
plot(x,y, xlab = "Sin", ylab = "Cos", type = "l")

enter image description here

如果我稍后添加积分,他们也不会工作。

theta = 1:3
x = sin(theta)
y = cos(theta)
op = par(bg = 'white', mar = rep(1, 4))
plot(x,y, xlab = "Sin", ylab = "Cos", type = "l")
plot.window(xlim = c(-1, 1), ylim = c(-1, 1))
points(x, y)

这是输出。 enter image description here

为什么输出会有这样的差异?

1 个答案:

答案 0 :(得分:2)

在绘制点plot.window之后和绘制线plot(...)导致不匹配之前,您设置了lines(...)限制。请尝试以下方法:

theta = 1:3
x = sin(theta)
y = cos(theta)
op = par(bg = 'white', mar = rep(1, 4))
plot.window(xlim = c(-1, 1), ylim = c(-1, 1))
plot(x,y, xlab = "Sin", ylab = "Cos", type = "p")
lines(x, y, col = hsv(0.95, 1, 1))