如何环绕ggplot2中的极坐标限制?
How to wrap around the polar coordinate limits in ggplot2?
我有一个圆形 space,其中 0 度角和 360 度角相等。我想在此 space 中绘制矩形,以便矩形可以穿过该值。但是,我在使用 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 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")
这显示了我想要的矩形,但由于笔划线的角度为 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 是相关的,也没有得到答复。
底层坐标系是极坐标是否很重要? 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()
这并没有解决您链接到的其他问题,但一般来说,您可能会发现自己将坐标从极坐标转换为欧几里德坐标会给您带来更大的灵活性,使绘图看起来像您想要的那样。
我有一个圆形 space,其中 0 度角和 360 度角相等。我想在此 space 中绘制矩形,以便矩形可以穿过该值。但是,我在使用 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")
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 是相关的,也没有得到答复。
底层坐标系是极坐标是否很重要? 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()
这并没有解决您链接到的其他问题,但一般来说,您可能会发现自己将坐标从极坐标转换为欧几里德坐标会给您带来更大的灵活性,使绘图看起来像您想要的那样。