【发布时间】: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