关闭ggplot2雷达/蜘蛛图中的线 [英] Closing the lines in a ggplot2 radar / spider chart
问题描述
我需要一种灵活的方式来制作ggplot2中的雷达/蜘蛛图。从我在github和ggplot2组中找到的解决方案中,我已经走到了这一步:
library(ggplot2)
#定义一个新的坐标系
coord_radar< - function(...){
结构(coord_polar(...),class = c(radar,polar )coord))
}
is.linear.radar< - function(coord)TRUE
#将所有变量重新标定为介于0和1之间
缩放< - as.data.frame(lapply(mtcars,ggplot2 ::: rescale01))
缩放$ model< - rownames(mtcars)#将模型名称添加为变量
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 ,变量==名称(缩放)[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)))
为了加入这些行,但这不起作用。这也不是:
关闭< - 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 =关闭)
这不能解决问题,并且还会产生大量的
geom_path:每组只包含一个观察值,您是否需要
调整组审美?
消息。索姆,我该怎么去关闭线路?
/ Fredrik对不起,我是愚蠢的。这似乎工作:
library(ggplot2)
#定义一个新的坐标系
coord_radar < - 函数(...){
结构(coord_polar(...),class = c(radar,polar,coord))
}
is.linear.radar< - function(coord)TRUE
#将所有变量重新标定为介于0和1之间
标定的< - as.data.frame(lapply(mtcars, ggplot2 ::: rescale01))
scaled $ model< - rownames(mtcars)#添加模型名称作为变量
as.data.frame(熔化(缩放, id.vars =model)) - > mtcarsm
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)))
I need a flexible way to make radar / spider charts in ggplot2. From solutions I've found on github and the ggplot2 group, I've come this far:
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)))
which works, except for the fact that lines are not closed. I thougth that I would be able to do this:
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)))
in order to join the lines, but this does not work. Neither does this:
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)
which does not solve the problem, and also produces lots of
"geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"
messages. Som, how do I go about closing the lines?
/Fredrik
Sorry, I was beeing stupid. This seems to work:
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
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)))
这篇关于关闭ggplot2雷达/蜘蛛图中的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!