0

I made pie charts embedded in a multiplot as on the picture below.

enter image description here

Now I'm struggling to force the same legend on each graph. I would like each legend to be exactly the same for each of the graph. Even when there is no bonus points for "3" in the data for a given year, I would still like to force the legend to indicate it, so as we have the same legend on each subplots.
Do you think it is possible?

Here is the code I have used to make these graphs.

year <- 2012:2014    
x <- data.frame(Year = c(2012, 2012, 2012, 2013, 2013, 2013, 2014, 2014, 2014, 2014), 
                      EE_TOK_points = c(0, 1, 2, 0, 1, 2, 0, 1, 2, 3), 
                      Num_cand = c(3, 12,  1,  5,  5,  4,  1,  9,  4,  1),
                      Num_coh = c(16, 16, 16, 14, 14, 14, 15, 15, 15, 15))



x <- x %>% mutate(Percentage = round(Num_Cand / Num_Coh * 100), 
              label_pos = cumsum(Percentage) - Percentage / 2,
              perc_text = paste0(round(Percentage), "%")) %>% 
      arrange(Year, desc(EE_TOK_points))
x <- x[,c("Year", "EE_TOK_points", "Percentage", "label_pos", "perc_text")]

plot = list()

for(i in 1:3){
  x3 <- x %>% filter(Year == year[i]) %>% arrange(EE_TOK_points)
  x3$EE_TOK_points <- factor(x3$EE_TOK_points)
  plot[[i]] <- ggplot(x3, aes(x = 1, y = Percentage, fill = EE_TOK_points)) + 
    geom_bar(stat = "identity", color = "black", width = 1) + 
    geom_text(aes(x = 1.25, y = label_pos, label = perc_text), size = 3.5) + 
    guides(fill=guide_legend(override.aes=list(colour=NA))) + 
    coord_polar(theta='y') + 
    scale_y_continuous(breaks = NULL) + 
    scale_fill_manual(values = c("#f6736c", "#79AE21", "#18BFC4", "#C878FC"), 
                      label = c("0", "1", "2", "3"), 
                      name = "Bonus \n Points") +  
    ggtitle(year[i]) + 
    theme(axis.ticks=element_blank(), 
        axis.title=element_blank(), 
        axis.text.y=element_blank(), 
        axis.text.x=element_text(colour='black')) + 
    theme(panel.background = element_rect(fill = "white"))
}

multiplot(plotlist = plot, cols = 2)

And here is what x3 would look like

> head(x3)
Source: local data frame [4 x 5]
Groups: Year [1]

   Year EE_TOK_points Percentage label_pos perc_text
  <int>        <fctr>      <dbl>     <dbl>     <chr>
1  2016             0         28      14.0       28%
2  2016             1         22      39.0       22%
3  2016             2         39      69.5       39%
4  2016             3         11      94.5       11%
> 

Thanks in advance for any ideas / suggestions on how to proceed.

Franky
  • 721
  • 3
  • 11
  • 19
  • Please include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610), this will make it a lot easier for others to help you. – Jaap Nov 06 '16 at 10:29
  • hi @ProcrastinatusMaximus, I have just added a first line to make it reproducible. Let me know if it works. – Franky Nov 06 '16 at 12:34
  • Factor `EE_TOK_points` in `x` before subsetting, then use `drop = FALSE` in `scale_fill_manual`. – aosmith Nov 07 '16 at 18:18

0 Answers0