Closing the lines in a ggplot2 radar / spider chart

后端 未结 6 1986
时光取名叫无心
时光取名叫无心 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:52

    • solution key factor
      1. add duplicated mpg row after melt by rbind
      2. inherit CoordPolar on ggproto
      3. set is_linear = function() TRUE on ggproto

    especially is_linear = function() TRUE is important,
    since if not you will get plot like this...

    with is_linear = function() TRUE settings you can get,

    library(dplyr)
    library(data.table)
    library(ggplot2)
    
    rm(list=ls())
    
    scale_zero_to_one <- 
      function(x) {
        r <- range(x, na.rm = TRUE)
        min <- r[1]
        max <- r[2]
        (x - min) / (max - min)
      }
    
    scaled.data <-
      mtcars %>%
      lapply(scale_zero_to_one) %>%
      as.data.frame %>%
      mutate(car.name=rownames(mtcars)) 
    
    plot.data <-
      scaled.data %>%
      melt(id.vars='car.name') %>%
      rbind(subset(., variable == names(scaled.data)[1]))
    
    # create new coord : inherit coord_polar
    coord_radar <- 
      function(theta='x', start=0, direction=1){
        # input parameter sanity check
        match.arg(theta, c('x','y'))
    
        ggproto(
          NULL, CoordPolar, 
          theta=theta, r=ifelse(theta=='x','y','x'),
          start=start, direction=sign(direction),
          is_linear=function() TRUE)
      }
    
    plot.data %>%
      ggplot(aes(x=variable, y=value, group=car.name, colour=car.name)) + 
      geom_path() +
      geom_point(size=rel(0.9)) +
      coord_radar() + 
      facet_wrap(~ car.name, nrow=4) + 
      theme_bw() +
      theme(
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.x = element_blank(),
        legend.position = 'none') +
      labs(title = "Cars' Status")
    
    • final result

提交回复
热议问题