0

I am trying to create a series pie-charts using ggplot where the size of the total pie for each individual chart is proportional to the total numeric size.

To generalise the problem, I have created this toy dataset - in which 4 tree were sampled for bugs, each in three locations in the canopy - upper, middle and lower.

Tree_ID <- as.factor(c("Tree_1","Tree_1","Tree_1","Tree_2","Tree_2","Tree_2","Tree_3","Tree_3","Tree_3","Tree_4","Tree_4","Tree_4"))
Count_loc <- as.factor(c("Upper", "Middle", "Lower", "Upper", "Middle", "Lower", "Upper", "Middle", "Lower","Upper", "Middle", "Lower"))
Bugs_per_loc  <- c(75000,   20000, 5000,    700,    250,    50, 50, 30, 20, 4,  4,  4)
Bugs_per_tree <- c(100000, 100000, 100000, 1000, 1000, 1000, 100, 100, 100, 12, 12, 12)
Ppt_bugs_loc <- c(.75, .20, .05, .70, .25, .05, .50, .30, .20, .333, .333, .333)
Tree_bug_counts <- data.frame(Tree_ID, Count_loc, Bugs_per_loc, Bugs_per_tree,  Ppt_bugs_loc)

With the following code, I can produce pie charts in ggplot, with each pie the same size

plot_1 <- ggplot(Tree_bugs, aes(x="", y=Ppt_bugs_loc,  fill=Count_loc)) + 
  geom_bar(stat="identity") + 
  facet_wrap(Tree_ID, ncol=2)   + 
  coord_polar("y", start=0) +
  theme_void()
plot_1

Plot 1 - pie charts with fixed pie sizes

When i then try to produce pies proportional to the total number of bugs sampled per tree using the "width" argument, I get meaningful plots, but with a "hole" in the smaller pies.


plot_2 <- ggplot(Tree_bugs, aes(x="", y=Ppt_bugs_loc, fill=Count_loc, width=log10(Bugs_per_tree))) +
  geom_bar(stat="identity") + 
  facet_wrap(Tree_ID, ncol=2)   + 
  coord_polar("y", start=0) +
  theme_void()
plot_2

Plot 2 - pie charts with somewhat proportionally sized pies

I think I can see what is causing the problem, as the width argument is initially producing a centered bar, and the white space to the left is carried forward when the bar plot is projected to polar co-ordinates.

However, I have not been able to find a work-around

Thanks

user2085797
  • 69
  • 1
  • 6

1 Answers1

0

Here's an approach using geom_rect and pre-calculating the stacking within each tree:

Tree_bug_counts %>%
  group_by(Tree_ID) %>%
  arrange(Tree_ID, Count_loc) %>%
  mutate(cuml_pct = cumsum(Ppt_bugs_loc)) %>%
  ungroup() %>%
  
  ggplot(aes(xmin=0, xmax = log10(Bugs_per_tree),
             ymin=cuml_pct - Ppt_bugs_loc, ymax = cuml_pct,
             fill=Count_loc)) + 
  geom_rect() +
  facet_wrap(Tree_ID, ncol=2)   + 
  coord_polar("y", start=0) +
  theme_void()

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53