【问题标题】:ggplot geom_line in polar coordinates connects strangely极坐标中的ggplot geom_line奇怪地连接
【发布时间】:2016-08-03 19:28:28
【问题描述】:

我想将r = theta 从 0 绘制到 20\pi,这应该是一个有十个循环的螺旋。

这工作正常:

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时,它奇怪地连接了点:

我该如何解决这个问题?

【问题讨论】:

  • 添加 %>% #arrange(r) %>%+ geom_path() 可以帮助您实现目标,但 geom_path 不会越过 0 线。令人费解...

标签: r ggplot2 polar-coordinates


【解决方案1】:

要做的关键是设置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)

【讨论】:

  • 所以诀窍是按 mod 2 pi 分组,并将落在边缘的点复制到每个组中。烦人,但它有效。
猜你喜欢
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 2018-02-27
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多