rggplot2

How to make an even radial plot in Ggplot2


I'm using the coord_radial of ggplot2, but I can't make the axis limits correct. The x aesthetic (in this case theta) is a factor and y (r) is a number. I want the first factor level to be exactly at 12 (north) and the other levels the be evenly distributed. In the example below, the gap between the first and the last levels (A and D) is larger than the other gaps. Among other things, I have tried using scale_x_discrete and expand. Below is a minimal example, but my real case has 15 levels.

library(dplyr)
library(ggplot2)

tibble(lab = c("A", "B", "C", "D"),
       val = rexp(4)) %>%
  ggplot(aes(lab, val, group = 1)) +
  geom_line() +
  coord_radial(r.axis.inside = TRUE)

Radial plot that is not evenly in four pieces.


Solution

  • Set expand last value to 1, meaning add 1 full space after last value "D".

    library(ggplot2)
    
    ggplot(data.frame(lab = c("A", "B", "C", "D"),
                      val = rexp(4)),
           aes(lab, val, group = 1)) +
      geom_line() +
      scale_x_discrete(expand = c(0, 0, 0, 1)) +
      coord_radial()
    

    enter image description here

    Test: Try running the code without coord_radial to see what the expand is doing.

    enter image description here