如何遍历scale_fill_cyclical并分配图例标签

时间:2017-09-25 00:01:49

标签: r ggplot2

如果我想在下面的链接中的加泰罗尼亚选举示例中模仿和循环结构,我该如何在scale_fill_cyclical()中指定图例标签?

https://cran.r-project.org/web/packages/ggjoy/vignettes/gallery.html

除了示例中的行数,我有几个月,还有12个场景要与历史记录进行比较,所以我正在寻找其中的12个图。为了进行比较,就好像我为每个情节保留了Indy(Indy将是我的历史情景),并使用Unionist作为个别场景for循环的插槽。

library(tidyverse)
library(forcats)
Catalan_elections %>%
mutate(YearFct = fct_rev(as.factor(Year))) %>%
ggplot(aes(y = YearFct)) +
geom_joy(aes(x = Percent, fill = paste(YearFct, Option)), 
         alpha = .8, color = "white", from = 0, to = 100) +
labs(x = "Vote (%)",
     y = "Election Year",
   title = "Indy vs Unionist vote in Catalan elections",
   subtitle = "Analysis unit: municipalities (n = 949)",
   caption = "Marc Belzunces (@marcbeldata) | Source: Idescat") +
scale_y_discrete(expand = c(0.01, 0)) +
scale_x_continuous(expand = c(0.01, 0)) +
scale_fill_cyclical(breaks = c("1980 Indy", "1980 Unionist"),
                  labels = c(`1980 Indy` = "Indy", `1980 Unionist` = "Unionist"),
                  values = c("#ff0000", "#0000ff", "#ff8080", "#8080ff"),
                  name = "Option", guide = "legend") +
theme_joy(grid = FALSE)

正如我所希望的那样,12个情节很好地与周期性填充绘制,只是比较情景的图例文本或“工会主义者”(蓝色文本)显示了月份和情景。就像1980年被任意选为传奇年代之一一样,我选择了九月作为我的,但不要在传说中。我怀疑报价格式是不对的,而且“paste1”是标签调用中的问题。它似乎正在正确读取中断,因为它正在周期性地正确缩放。

 scenarios <- c("hist", "e1", "e2", "e3", "e4", "e5", "cwc30", "l1", "l2", "l3", "l4", "l5", "cwc70")
 paste1 <- paste0("Sept"," ",scenarios[i]) 
如上所述

因子设置和ggplot和geom调用,然后:

 scale_fill_cyclical(breaks= c("Sept hist", paste1),                 
              labels = c(`Sept hist` = "Historical (Q0)",  `paste1` = paste(scenarios[i])),                  
              values = c("#ff0000", "#0000ff", "#ff8080", "#8080ff"),
              name = "scenario", guide = "legend")

简而言之,我只想在传奇中使用场景“e2”,而不是“Sept e2”。

enter image description here

0 个答案:

没有答案