I'm trying to show two variables using ggplot2's polar_coord
, one variable would be reflected in the width (i.e., the length of the crust) of the pie slices and the second variable in the radius (i.e., the length of the non-crust sides) of the slices, is this possible?
In the example below, I'd like cat1 to be 4/20 (4+6+10) of the pie's circumference with a radius of 1, cat2 to be 6/20 of the circumference with a radius of 4, and cat3 to be 10/20 of the circumference with a radius of 5.
df <- data.frame(
x = c("cat1", "cat2", "cat3"),
y = c(4, 6, 10),
z = c(1, 4, 5))
The closest I've gotten is slices that are the correct size relative to each other (code below), but I'd like for the slices to fill out the entire pie and not be constrained by a max of a quarter of the total pie circumference.
cxc <- ggplot(df, aes(x = x,
y = z,
width = y)) +
geom_bar(aes(fill=x), stat="identity", color = "black")
cxc + coord_polar()
Does this do what you want?
library(dplyr)
df %>%
mutate(xmin = cumsum(lag(y, default = 0)),
xmax = cumsum(y)) %>%
ggplot(aes(xmin = xmin, xmax = xmax,
ymin = 0, ymax = z,
fill = x)) +
geom_rect(colour = "black") +
coord_polar()