【发布时间】:2015-03-06 11:47:54
【问题描述】:
我需要一种灵活的方式在 ggplot2 中制作雷达/蜘蛛图。从我在 github 和 ggplot2 小组中找到的解决方案中,我已经走到了这一步:
library(ggplot2)
# Define a new coordinate system
coord_radar <- function(...) {
structure(coord_polar(...), class = c("radar", "polar", "coord"))
}
is.linear.radar <- function(coord) TRUE
# rescale all variables to lie between 0 and 1
scaled <- as.data.frame(lapply(mtcars, ggplot2:::rescale01))
scaled$model <- rownames(mtcars) # add model names as a variable
as.data.frame(melt(scaled,id.vars="model")) -> mtcarsm
ggplot(mtcarsm, aes(x = variable, y = value)) +
geom_path(aes(group = model)) +
coord_radar() + facet_wrap(~ model,ncol=4) +
theme(strip.text.x = element_text(size = rel(0.8)),
axis.text.x = element_text(size = rel(0.8)))
这行得通,除了行没有关闭。 我认为我能够做到这一点:
mtcarsm <- rbind(mtcarsm,subset(mtcarsm,variable == names(scaled)[1]))
ggplot(mtcarsm, aes(x = variable, y = value)) +
geom_path(aes(group = model)) +
coord_radar() + facet_wrap(~ model,ncol=4) +
theme(strip.text.x = element_text(size = rel(0.8)),
axis.text.x = element_text(size = rel(0.8)))
为了加入行,但这不起作用。这也不是:
closes <- subset(mtcarsm,variable == names(scaled)[c(1,11)])
ggplot(mtcarsm, aes(x = variable, y = value)) +
geom_path(aes(group = model)) +
coord_radar() + facet_wrap(~ model,ncol=4) +
theme(strip.text.x = element_text(size = rel(0.8)),
axis.text.x = element_text(size = rel(0.8))) + geom_path(data=closes)
解决不了问题,还产生很多
"geom_path: 每组只包含一个观察值。你需要 调整群体审美?”
消息。 Som,我该如何关闭线路?
/弗雷德里克
【问题讨论】:
-
ggplot(mtcarsm[mtcarsm$model == "Maserati Bora", ], aes(x = variable, y = value)) + geom_path(aes(group = model)) + coord_radar()关闭此处的行。 -
现在我很困惑。运行您发布的代码清楚地显示了
mpg和carb之间的差距。这是怎么回事? -
忘了说:关闭
rbinded 数据框的行。
标签: r ggplot2 radar-chart aesthetics