Created
April 4, 2019 05:13
-
-
Save kzktmr/5e36672816acc8c08a7fce499b4a5ca9 to your computer and use it in GitHub Desktop.
Draw a radar chart with ggplot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
# http://www.cmap.polytechnique.fr/~lepennec/R/Radar/RadarAndParallelPlots.html | |
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) | |
} | |
# https://qiita.com/yutannihilation/items/f30baef75a0ac02bb2f0 | |
GeomRadar <- ggproto("GeomRadar", GeomPolygon, | |
default_aes = aes(colour = "black", fill = NA, size = 0.5, linetype = 1, alpha = NA), | |
draw_key = draw_key_path | |
) | |
geom_radar <- function(mapping = NULL, data = NULL, stat = "identity", | |
position = "identity", na.rm = FALSE, show.legend = NA, | |
inherit.aes = TRUE, ...) { | |
layer( | |
stat = stat, geom = GeomRadar, data = data, mapping = mapping, | |
position = position, show.legend = show.legend, inherit.aes = inherit.aes, | |
params = list(na.rm = na.rm, ...) | |
) | |
} | |
n <- 10 | |
m <- 5 | |
dat <- tibble(item = rep(letters[1:n], each = m), | |
index = rep(LETTERS[1:m], n), | |
value = runif(n * m)) | |
str(dat) | |
dat %>% ggplot(aes(x = index, y = value, color = item, group = item)) + | |
geom_radar() + geom_point() + coord_radar() + | |
scale_x_discrete(expand = expand_scale(add = c(0, 1))) + | |
theme_bw(base_family = "Palatino") + theme(axis.title = element_blank()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was a really helpful bit of code an produced a much better set of figures than other methods I had found.