Skip to content

Instantly share code, notes, and snippets.

@kzktmr
Created Apr 4, 2019
Embed
What would you like to do?
Draw a radar chart with ggplot.
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())
@jasonpott

This comment has been minimized.

Copy link

@jasonpott jasonpott commented Aug 1, 2020

This was a really helpful bit of code an produced a much better set of figures than other methods I had found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment