在极坐标下,用直线连接数据点的图形也被称为
雷达图。
Erwan Le Pennec撰写了一篇名为“从平行坐标到雷达图”的文章,介绍如何使用
ggplot2
创建雷达图。他建议使用
coord_radar()
函数来定义。文章链接:
From Parallel Plot to Radar Plot。
coord_radar <- function (theta = "x", start = 0, direction = 1) {
theta <- match.arg(theta, c("x", "y"))
r <- if (theta == "x") "y" else "x"
ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start,
direction = sign(direction),
is_linear = function(coord) TRUE)
}
通过这个,我们可以创建如下的图表:
library(tidyr)
library(dplyr)
library(ggplot2)
iris %>% gather(dim, val, -Species) %>%
group_by(dim, Species) %>% summarise(val = mean(val)) %>%
ggplot(aes(dim, val, group=Species, col=Species)) +
geom_line(size=2) + coord_radar()
coord_radar()
是 ggiraphExtra
包的一部分。因此,您可以直接使用它。
iris %>% gather(dim, val, -Species) %>%
group_by(dim, Species) %>% summarise(val = mean(val)) %>%
ggplot(aes(dim, val, group=Species, col=Species)) +
geom_line(size=2) + ggiraphExtra:::coord_radar()
请注意,
coord_radar()
函数不被该软件包导出。因此,需要使用三个冒号(
:::
)来访问该函数。