Reputation: 1031
I would like to create pie charts with different radii in separate facets. How can I modify the following code (which produces equal radii) so the variable total is th rasdius for each slice?
library(tidyverse)
mydf <- data_frame(value=c(1:3,1:3),
total=rep(c(10,20),times=3),
cond=rep(c("x","y","z"),times=2),
group=rep(c("a","b"),each=3))
mydf %>% ggplot(aes(x="",y=value,fill=cond)) +
geom_bar(stat="identity",width=1) +
facet_wrap(~ group) +
coord_polar("y")
Upvotes: 0
Views: 1172
Reputation: 661
This an old question but I wanted to do the same thing: The pie radius should vary with the group total.
First of all, I use geom_bar(position = "fill")
so that the two group bars stack up to the same height. Then I use the group totals as a width aesthetic: ggplot(aes(x = total/2, width = total))
. Using half the total for the x aesthetic ensures that we get proper pies not doughnuts.
So, the following code works for me although I'm not sure about how much of this is rather a hack than a proper solution (ggplot issues a warning if we move the width
aesthetic into the geom_bar
call: geom_bar(aes(width = total))
).
library(tidyverse)
mydf <- tibble(group = rep(c("group a", "group b"), each = 3),
cond = rep(c("x", "y", "z"), times = 2),
value = c(1, 2, 3, 2, 4, 6)) %>%
group_by(group) %>%
add_tally(value, name = "total") %>%
ungroup() %>%
mutate(label = sprintf("total = %d", total)) %>%
print()
#> # A tibble: 6 x 5
#> group cond value total label
#> <chr> <chr> <dbl> <dbl> <chr>
#> 1 group a x 1 6 total = 6
#> 2 group a y 2 6 total = 6
#> 3 group a z 3 6 total = 6
#> 4 group b x 2 12 total = 12
#> 5 group b y 4 12 total = 12
#> 6 group b z 6 12 total = 12
mydf %>% ggplot(aes(x = total/2, y = value, fill = cond, width = total)) +
geom_bar(stat = "identity", position = "fill", color = "white") +
facet_wrap(~ group + label, strip.position = "bottom") +
coord_polar("y", start = 0, direction = -1) +
theme_bw(base_size = 12) +
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
legend.title = element_text(size = 14),
strip.background = element_rect(fill = NA, colour = NA),
strip.text = element_text(size = 16))
Created on 2020-02-13 by the reprex package (v0.3.0)
To better understand how this works, we can remove coord_polar()
and the theme()
calls to have a look at the underlying bar chart:
mydf %>% ggplot(aes(x = total/2, y = value, fill = cond, width = total)) +
geom_bar(stat = "identity", position = "fill", color = "white") +
facet_wrap(~ group + label, strip.position = "bottom") +
theme_bw(base_size = 12)
Upvotes: 1
Reputation: 4989
Your question is a bit unclear. Something like that?
library(tidyverse)
mydf <- data_frame(value = c(1:3, 1:3),
total = rep(c(10, 20), times = 3),
cond = rep(c("x", "y", "z"),times = 2),
group = rep(c("a", "b"), each = 3))
mydf %>% ggplot(aes(x = "", y = value, fill = cond)) +
geom_bar(stat = "identity", width = 1) +
facet_wrap(total ~ cond) +
coord_polar("x")
Upvotes: 0