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)
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)
Share
Improve this question
asked Feb 5 at 9:19
MankkaMankka
5631 gold badge3 silver badges14 bronze badges
0
3 Answers
Reset to default 2Set 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()
Test: Try running the code without coord_radial
to see what the expand is doing.
If you want a gap between the first and last levels, then you can add scale_x_discrete(expand = expansion(mult = xxx)))
to adjust the gap between the data and the axes. The defaults are to expand the scale by 5% on each side for continuous variables, and by 0.6 units on each side for discrete variables (see the help page for scale_x_discrete
).
It's not clear to me how much to expand (it largely depends on how many discrete categories you have), but with a little trial and error, you may find the best looking plot.
set.seed(10)
n <- 15
tibble(lab = LETTERS[1:n], val = rexp(n)) %>%
ggplot(aes(lab, val, group = 1)) +
geom_line() +
coord_radial(r.axis.inside = TRUE) +
scale_x_discrete(expand = expansion(mult = c(0, 0.08))
The different gaps between the levels are caused by expand
defaulting to TRUE
, on coord_radial()
. So, by setting expand = FALSE
, you get equal gaps:
tibble(lab = LETTERS[1:4],
val = rexp(4)) %>%
ggplot(aes(lab, val, group = 1)) +
geom_line() +
coord_radial(r.axis.inside = TRUE,
expand = FALSE)
The problem now is that the first and last levels are on the same place. My not-so-elegant solution to this is to add an extra lab
on the data, with NA
as a value, and then set the breaks
on scale_x_discrete()
for it not to be plotted.
tibble(lab = LETTERS[1:4],
val = rexp(4)) %>%
add_row(lab = "E", val = NA) %>%
ggplot(aes(lab, val, group = 1)) +
geom_line() +
scale_x_discrete(breaks = c("A", "B", "C", "D")) +
coord_radial(r.axis.inside = TRUE,
expand = FALSE)