Closing the lines in a ggplot2 radar / spider chart

后端 未结 6 1985
时光取名叫无心
时光取名叫无心 2020-12-16 07:11

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(ggpl         


        
6条回答
  •  佛祖请我去吃肉
    2020-12-16 07:51

    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))) 
    

提交回复
热议问题