Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

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

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

enter image description here

A previous workaround for this no longer works. Anyone have any ideas for an updated workaround?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...