gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:36:51 27 4
gpt4 key购买 nike

我有一个圆形空间,其中角度 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)

enter image description here

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")

enter image description here
这至少产生了一个情节,但情节与我想要的相反。这不是从 340 到 380 CCW,而是绘制 340 到 20 CW。

3. 尝试绘制为两个相连的元素
  base + geom_rect(aes(xmin = c(350, 0), xmax = c(360, 10), ymin = 0.4, ymax = 0.6), 
color = "darkblue", fill = "steelblue")

enter image description here

这显示了我想要的矩形,但由于笔划线的角度为 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")

enter image description here
这似乎失去了缩放和限制。

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")

enter image description here
这可以正确完成缩放,但会覆盖极坐标系。

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

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

最佳答案

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

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()

enter image description here

这并不能解决您链接到的其他问题,但一般而言,您可能会发现自己将坐标从极坐标转换为欧几里得可以为您提供更大的灵活性,使绘图看起来像您想要的样子。

关于r - 如何环绕ggplot2中的极坐标限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47760020/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com