我正在尝试在 ggplot2 中使用不是径向线的注释线制作极坐标直方图。
coord_polar
给出曲线的简单方法:
library(ggplot2)
d = data.frame(x=rep(seq(0, 350, 10), times=1:36))
lines = data.frame(x = c(40, 90, 150, 220, 270),
y = c(20, 20, 20, 20, 20),
xend = c(115, 165, 225, 295, 345),
yend = c(5, 5, 5, 5, 5))
ggplot(d, aes(x)) +
geom_histogram(binwidth = 10) +
geom_segment(data = lines,
aes(x, y, xend = xend, yend = yend),
color = 'red') +
coord_polar() +
scale_x_continuous(limits=c(0, 360))
第二次尝试使用coord_radar
,来自 StackOverflow 和邮件列表上的各种来源:
coord_radar <- function (theta = "x", start = 0, direction = 1)
{
theta <- match.arg(theta, c("x", "y"))
r <- if (theta == "x")
"y"
else "x"
ggproto("CoordRadar", CoordPolar, theta = theta, r = r, start = start,
direction = sign(direction),
is_linear = function(coord) TRUE)
}
ggplot(d, aes(x)) +
geom_histogram(binwidth = 10) +
geom_segment(data = lines,
aes(x, y, xend = xend, yend = yend),
color = 'red') +
coord_radar()
这完全失败了:
如果我使用分组线而不是线段,我可以绘制线条:
lines2 = data.frame(x = c(40, 115, 90, 165, 150, 225, 220, 295, 270, 345, 330, 45),
y = c(20, 5, 20, 5, 20, 5, 20, 5, 20, 5, 20, 5),
group = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6))
ggplot(lines2, aes(x, y, group = group)) +
geom_line(color = 'red') +
coord_radar() +
scale_y_continuous(limits = c(0, 36)) +
scale_x_continuous(limits = c(0, 360))
但我仍然需要直方图...
有任何想法吗?