极坐标中的ggplot geom_line奇怪地连接
ggplot geom_line in polar coordinates connects strangely
我想绘制从 0 到 20\pi 的 r = theta
,这应该是一个有十个循环的螺旋线。
这工作正常:
data.frame(x=seq(0,20*pi, length.out=1000)) %>% mutate(theta=x %% (2*pi), r=x) %>%
ggplot() + aes(x=theta, y=r) + coord_polar(start=-pi/2, direction=-1) +
ggtitle("r=theta") + geom_line() + ylim(0,20*pi) + xlim(0, 2*pi)
但是当我将 geom_point
更改为 geom_line
时,它奇怪地连接了这些点:
我该如何解决这个问题?
要做的关键是设置 group
美学以阻止行与 geom_path
加倍。在这里我设置了一些不同的东西以避免在 theta = 0
处出现间隙
data.frame(theta = rep(seq(0, 2 * pi, length = 100), 10)) %>%
mutate(r = seq(0, 20 * pi, length = 1000), z = rep(1:10, each = 100)) %>%
ggplot() + aes(x=theta, y=r, group = z) +
coord_polar(start = -pi/2, direction = -1) +
ggtitle("r = theta") +
geom_path() +
ylim(0, 20 * pi) + xlim(0, 2 * pi)
我想绘制从 0 到 20\pi 的 r = theta
,这应该是一个有十个循环的螺旋线。
这工作正常:
data.frame(x=seq(0,20*pi, length.out=1000)) %>% mutate(theta=x %% (2*pi), r=x) %>%
ggplot() + aes(x=theta, y=r) + coord_polar(start=-pi/2, direction=-1) +
ggtitle("r=theta") + geom_line() + ylim(0,20*pi) + xlim(0, 2*pi)
但是当我将 geom_point
更改为 geom_line
时,它奇怪地连接了这些点:
我该如何解决这个问题?
要做的关键是设置 group
美学以阻止行与 geom_path
加倍。在这里我设置了一些不同的东西以避免在 theta = 0
data.frame(theta = rep(seq(0, 2 * pi, length = 100), 10)) %>%
mutate(r = seq(0, 20 * pi, length = 1000), z = rep(1:10, each = 100)) %>%
ggplot() + aes(x=theta, y=r, group = z) +
coord_polar(start = -pi/2, direction = -1) +
ggtitle("r = theta") +
geom_path() +
ylim(0, 20 * pi) + xlim(0, 2 * pi)