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

ggplot2, r

Solution

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.

Problem

`coord_polar` curves lines, sometimes when you may not wish to (i.e. when the space is considered discrete not continuous): ``` 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_polar() ``` A previous workaround for this no longer works. Anyone have any ideas for an updated workaround?

Original source

Related problems