作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个圆形空间,其中角度 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)
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).
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")
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")
最佳答案
底层坐标系是极坐标是否重要? 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()
关于r - 如何环绕ggplot2中的极坐标限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47760020/
我正在寻找绘制极坐标数据的替代方法。我需要实现像 this 这样的图表具有动态选项,例如 this . 非常感谢您的帮助! 最佳答案 我个人需要这些: Highcharts JS canvasXpre
我是一名优秀的程序员,十分优秀!