【问题标题】:How to wrap around the polar coordinate limits in ggplot2?如何环绕ggplot2中的极坐标限制?
【发布时间】:2017-12-11 19:29:07
【问题描述】:

我有一个圆形空间,其中角度 0 和 360 是等价的。我想在这个空间中绘制矩形,以便矩形可以穿过这个值。但是,我在使用 ggplot2 时遇到了问题。

base <- ggplot() +
  scale_x_continuous(breaks = seq(45, 360, 45), limits = c(0, 360)) +
  scale_y_continuous(breaks = seq(0, 1, 0.2), limits = c(0, 1)) +
  coord_polar(theta = "x", start = 1.5 * pi, direction = -1)

1.尝试使用超过 xlim 的值进行绘图:

base + geom_rect(aes(xmin = 340, xmax = 380, ymin = 0.4, ymax = 0.6), 
  color = "darkblue", fill = "steelblue")
#> Warning message:
#> Removed 1 rows containing missing values (geom_rect). 

xlim 之外的所有值都被删除,因此这不起作用。

2。尝试使用重新缩放的值进行绘图

base + geom_rect(aes(xmin = 340, xmax = 380 %% 360, ymin = 0.4, ymax = 0.6), 
  color = "darkblue", fill = "steelblue")

这至少产生了一个情节,但情节与我想要的相反。这不是从 340 到 380 逆时针,而是从 340 到 20 顺时针。

3.尝试绘制为两个连接元素

  base + geom_rect(aes(xmin = c(350, 0), xmax = c(360, 10), ymin = 0.4, ymax = 0.6), 
    color = "darkblue", fill = "steelblue")

这显示了我想要的矩形,但这作为解决方案并不令人满意,因为笔划线的角度为 0/360,而且我现在必须将每个矩形表示为两个矩形。

4.尝试 1 使用缩放而不是剪辑

ggplot() +
  scale_x_continuous(breaks = seq(45, 360, 45)) +
  scale_y_continuous(breaks = seq(0, 1, 0.2), limits = c(0, 1)) +
  coord_cartesian(xlim = c(0, 360)) +
  coord_polar(theta = "x", start = 1.5 * pi, direction = -1) +
  geom_rect(aes(xmin = 340, xmax = 380, ymin = 0.4, ymax = 0.6), 
    color = "darkblue", fill = "steelblue")

这似乎失去了缩放和限制。

5.尝试 2 使用缩放而不是剪辑

ggplot() +
  scale_x_continuous(breaks = seq(45, 360, 45)) +
  scale_y_continuous(breaks = seq(0, 1, 0.2), limits = c(0, 1)) +
  coord_polar(theta = "x", start = 1.5 * pi, direction = -1) +
  coord_cartesian(xlim = c(0, 360)) +
  geom_rect(aes(xmin = 340, xmax = 380, ymin = 0.4, ymax = 0.6), 
    color = "darkblue", fill = "steelblue")

这样可以正确完成缩放,但会覆盖极坐标系。

如果有人能为这个问题提供解决方案或想法,我将不胜感激。同样,我正在寻找看起来像 #3 但没有内部笔划且不需要使用两个矩形的东西。

编辑:这个question 是相关的,也没有得到答复。

【问题讨论】:

  • 在示例 3 中,您可以通过删除 color = "darkblue", 并仅使用 fill = 来消除 0/360 处的笔划线。不能解决绘制两个矩形的需要,但不幸的是 ggplot 中的某些东西仍然需要解决方法。
  • 我也有同样的想法。如果没有其他解决方案出现,我将不得不这样做。

标签: r ggplot2 polar-coordinates


【解决方案1】:

底层坐标系是极坐标是否重要? ggforce 包中的 geom_arc_bar() 的行为与您预期的一样,因此您可以使用它以任意角度绘制弧线。但是您在下面有一个笛卡尔坐标系,因此如果需要,您可能必须自己绘制坐标线。

library(ggforce)
library(dplyr)

data_deg <- data.frame(xmin = 340,
                   xmax = 380,
                   ymin = 0.4,
                   ymax = 0.6)

offset = 90 # by how much are angles offset
dir = 1 # should we go counterclockwise (1) or clockwise (-1)

# convert angles from degrees into radians, apply offset and direction
data_rad <- mutate(data_deg,
               xmin = dir*2*pi*(xmin + offset)/360,
               xmax = dir*2*pi*(xmax + offset)/360)

ggplot(data_rad) + geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = ymin, r = ymax,
                                start = xmin, end = xmax),
                            color = "darkblue", fill = "steelblue") +
  scale_x_continuous(limits = c(-1, 1)) +
  scale_y_continuous(limits = c(-1, 1)) +
  coord_fixed()

这并不能解决您链接到的其他问题,但总的来说,您可能会发现自己将坐标从极坐标转换为欧几里得坐标可以让您更灵活地使绘图看起来像您想要的那样。

【讨论】:

  • 这可能正是我需要的。感谢您的洞察力!
  • 另见herehere。我非常喜欢在笛卡尔坐标中绘制极坐标图。
  • 我怀疑这也将更容易自定义标签、网格等的位置和外观。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多