我将饼图嵌入多图中,如下图所示。
现在我正努力在每张图上强制使用相同的图例。我希望每个图表的每个图例都完全相同。即使在给定年份的数据中没有“3”的奖励积分,我仍然希望强制图例指示它,因为我们在每个子图上都有相同的图例。
你认为有可能吗?
这是我用来制作这些图表的代码。
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)
这就是x3的样子
> 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%
>
提前感谢有关如何继续的任何想法/建议。