ggplot2: connecting points in polar coordinates with a straight line 2

后端 未结 1 680
借酒劲吻你
借酒劲吻你 2020-12-01 22:47

coord_polar curves lines, sometimes when you may not wish to (i.e. when the space is considered discrete not continuous):

iris %>% gather(dim         


        
相关标签:
1条回答
  • 2020-12-01 23:44

    A plot in polar coordinates with data points connected by straight lines is also called a radar plot.

    There's an article by Erwan Le Pennec: From Parallel Plot to Radar Plot dealing with the issue to create a radar plot with ggplot2.

    He suggests to use coord_radar() defined as:

    coord_radar <- function (theta = "x", start = 0, direction = 1) {
      theta <- match.arg(theta, c("x", "y"))
      r <- if (theta == "x") "y" else "x"
      ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start, 
              direction = sign(direction),
              is_linear = function(coord) TRUE)
    }
    

    With this, we can create the plot as follows:

    library(tidyr)
    library(dplyr)
    library(ggplot2)
    
    iris %>% gather(dim, val, -Species) %>%
      group_by(dim, Species) %>% summarise(val = mean(val)) %>% 
      ggplot(aes(dim, val, group=Species, col=Species)) + 
      geom_line(size=2) + coord_radar()
    

    coord_radar() is part of the ggiraphExtra package. So, you can use it directly

    iris %>% gather(dim, val, -Species) %>%
          group_by(dim, Species) %>% summarise(val = mean(val)) %>% 
          ggplot(aes(dim, val, group=Species, col=Species)) + 
          geom_line(size=2) + ggiraphExtra:::coord_radar()
    

    Note that coord_radar() is not exported by the package. So, the triple colon (:::) is required to access the function.

    0 讨论(0)
提交回复
热议问题