我倾向于同意 Dave2e ,但我在尝试编写代码时学到了一些东西。
即使是 ggplot2 中的基本雷达图也不明显。
首先,我必须重复自行车数据,以便 geom_path 知道关闭多边形。
然后我不得不稍微修改coord_polar() 以强制使用直线。
然后我使用rayshader 包将其制作为 3D。
这里有一个问题是您需要使用 guides() 函数(而不是 scale_color_continuous(guide = "none") 来关闭指南。
library(ggplot2)
library(rayshader)
df <- data.frame(
YearMonth = c(202101L,202101L,202101L,
202102L,202102L,202102L,202103L,202103L,202103L),
Product = c("bike","car","skateboard",
"bike","car","skateboard","bike","car","skateboard"),
Sales = c(100L, 40L, 60L, 70L, 30L, 50L, 50L, 20L, 30L)
)
df <- rbind(df, subset(df, subset = Product == "bike"))
df$height <- match(df$YearMonth, sort(unique(df$YearMonth)))
df
# Define a new coordinate system from coord_polar
coord_radar <- function(theta = "x", start = 0, direction = 1, clip = "on") {
theta <- match.arg(theta, c("x", "y"))
r <- if (theta == "x")
"y"
else "x"
ggproto(NULL, CoordPolar, theta = theta, r = r, start = start,
direction = sign(direction), clip = clip,
# This is the change to make the lines straight
is_linear = function() TRUE
)
}
plot2d <- ggplot(df, aes(x = Product, y = Sales, color = height)) +
geom_path(aes(group = YearMonth)) +
scale_color_continuous() +
guides(color = "none") +
coord_radar()
plot_gg(plot2d, raytrace = FALSE)
给: