I've prepared a set of piechart using ggplot2 and facet_wrap. Despite the indication to drop NA values, in the final plot two pies show a blank slice. Is there a way to remove it? I have also tried with the filter option, removing values below a certain treshold, but it only works if a set the treshold value from 5 and above, but doing so I also remove values that I need. Any suggestion? Many thanks
here is my code:
#libraries
library(tidyverse)
library(dplyr)
library(ggplot2)
#dataset
B1_mf <- c(28, 5, NA, NA, 2, 58, 8)
B2_mf <- c(27, NA, 8, NA, 2, 56, 8)
B3_mf <- c(NA,NA,34,NA, 1, 59, 6)
B4_mf <- c(NA,NA,NA,45, 1, 49, 5)
ing <- c("ing_A", "ing_B", "ing_C", "ing_D", "ing_E", "ing_F", "ing_G")
B_df <- data.frame(ing, B1_mf, B2_mf, B3_mf, B4_mf)
#piechart
B_df %>%
gather(key = "type_n", value = "mf", B1_mf, B2_mf, B3_mf, B4_mf) %>%
drop_na(mf) %>%
ggplot(aes(x = 2,y = mf , fill = ing)) +
geom_bar(stat = "identity", color = "black", size = 0.2) +
facet_wrap(~type_n, ncol = 4, scales = "fixed") +
scale_fill_manual(values = c ("#c31e23", "#d85f52", "#e79184","#efc1b9", "#abbec8", "#658ea1", "#00607b")) +
coord_polar(theta = "y", direction = -1) +
geom_text(aes(label = mf), position = position_stack(vjust = 0.5), color = "white", size = 2) +
theme_light() +
theme(axis.text.y = element_blank(),
axis.text.x =element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position="bottom",
legend.key.height = unit(0.4, 'cm'),
legend.text = element_text(size =9),
legend.title = element_blank(),
strip.background = element_blank(),
strip.text = element_blank()) +
xlim(1, 2.5)
All you need to do is set scales = "free_y"
in your facet_wrap
statement. At present you are forcing all facets to use the same y (i.e. angle) limits.