Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an even radial plot in Ggplot2

Tags:

r

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.

like image 393
Mankka Avatar asked Nov 03 '25 08:11

Mankka


1 Answers

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

like image 173
zx8754 Avatar answered Nov 05 '25 02:11

zx8754



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!