R 如何环绕ggplot2中的极坐标限制?

R 如何环绕ggplot2中的极坐标限制?,r,ggplot2,polar-coordinates,R,Ggplot2,Polar Coordinates,我有一个圆形空间,其中角度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

我有一个圆形空间,其中角度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)
xlim之外的所有值都将被删除,因此这不起作用

2。尝试使用重新缩放的值进行打印

base + geom_rect(aes(xmin = 340, xmax = 380 %% 360, ymin = 0.4, ymax = 0.6), 
  color = "darkblue", fill = "steelblue")
  base + geom_rect(aes(xmin = c(350, 0), xmax = c(360, 10), ymin = 0.4, ymax = 0.6), 
    color = "darkblue", fill = "steelblue")
这至少产生了一个情节,但情节与我想要的相反。该图不是从340到380 CCW,而是绘制340到20 CW

3。尝试绘制为两个相连的元素

base + geom_rect(aes(xmin = 340, xmax = 380 %% 360, ymin = 0.4, ymax = 0.6), 
  color = "darkblue", fill = "steelblue")
  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")
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")
这似乎失去了缩放和限制

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_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")
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,但没有内部笔划,也不需要使用两个矩形


编辑:这是相关的,也没有答案。

底层坐标系是极坐标系是否至关重要<来自
ggforce
软件包的code>geom\u arc\u 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()
库(ggforce)
图书馆(dplyr)

在示例3中,可以通过删除
color=“darkblue”、
并仅使用
fill=
删除0/360处的笔划线。虽然不能解决绘制两个矩形的问题,但不幸的是,ggplot中的一些东西仍然需要解决。我也有同样的想法。如果没有其他的解决方案,我将不得不这样做。这可能正是我所需要的。谢谢你的洞察力!另见和。我非常喜欢在笛卡尔坐标系下绘制极坐标图。我想这也会使自定义标签、网格等的位置和外观变得更容易。