簡體   English   中英

scale_colour_grey() 不允許圖例標簽覆蓋,而是顯示舊的和新的

[英]scale_colour_grey() doesn't permit legend label overwrite, shows both old and new instead

我已經嘗試了 2 個小時來解決這個可能很簡單的問題。 這可能不是重復的 - 因為我找不到關於形狀和顏色與 scale_colour_grey 結合使用以覆蓋圖例標簽的情況的問題/答案

我也知道代碼不僅僅是次優(我將它用於各種不同圖形的模板)並且很樂意接受其他改進,但我想解決手頭的問題:

我正在嘗試生成由下面的代碼輸出的圖形,嘗試使用labels=c("Podemos-PCE/IU", "Podemos-PSOE", "PSOE-PCE/IU")覆蓋標簽。 一旦應用了 scale_colour_grey(),我的圖例就會被分割,我無法覆蓋它。 相反,它將新標簽放在舊標簽旁邊。

數據:

compBtwLeftParties <- structure(list(date = structure(c(16344, 16526, 16709, 16892, 
16983, 17167, 17348, 17532, 17713, 17805, 17897, 16344, 16526, 
16709, 16892, 16983, 17167, 17348, 17532, 17713, 17805, 17897, 
3103, 3804, 4838, 5448, 5660, 5813, 5903, 5995, 6360, 6574, 6665, 
6940, 7244, 7609, 7821, 8552, 8797, 9100, 9282, 9556, 10347, 
10773, 10988, 11931, 12570, 12784, 13118, 13239, 13899, 14153, 
14365, 14426, 14518, 14669, 14791, 15065, 15248, 15371, 15522, 
15614, 15887, 16161, 16344, 16526, 16709, 16892, 16983, 17167, 
17348, 17532, 17713, 17805, 17897), class = "Date"), party = c("PODEMOSPSOE", 
"PODEMOSPSOE", "PODEMOSPSOE", "PODEMOSPSOE", "PODEMOSPSOE", "PODEMOSPSOE", 
"PODEMOSPSOE", "PODEMOSPSOE", "PODEMOSPSOE", "PODEMOSPSOE", "PODEMOSPSOE", 
"PODEMOSPCEIU", "PODEMOSPCEIU", "PODEMOSPCEIU", "PODEMOSPCEIU", 
"PODEMOSPCEIU", "PODEMOSPCEIU", "PODEMOSPCEIU", "PODEMOSPCEIU", 
"PODEMOSPCEIU", "PODEMOSPCEIU", "PODEMOSPCEIU", "PSOEPCEIU", 
"PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", 
"PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", 
"PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", 
"PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", 
"PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", 
"PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", 
"PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", 
"PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", 
"PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", 
"PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", "PSOEPCEIU", 
"PSOEPCEIU", "PSOEPCEIU"), competition = c(7.82, 7.83, 7.85, 
7.72, 7.6, 7.44, 7.88, 7.69, 8.01, 8.01, 8, 9.76, 9.69, 9.68, 
9.91, 9.96, 9.94, 9.93, 9.92, 9.86, 9.88, 9.9, 8.36, 8.64, 8.66, 
8.54, 8.22, 8.29, 8.24, 8.22, 8.13, 8.13, 8.14, 8.09, 7.97, 8.07, 
8.02, 8.02, 7.63, 7.74, 7.86, 8, 8.18, 8.15, 8.09, 8.04, 8.33, 
8.29, 8.35, 8.41, 8.36, 8.35, 8.36, 8.44, 8.33, 8.39, 8.42, 8.33, 
8.24, 8.28, 8.32, 8.19, 8.25, 8.17, 8.06, 8.14, 8.17, 7.81, 7.56, 
7.38, 7.81, 7.61, 7.87, 7.89, 7.9), mean1 = c(8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333, 8.34293333333333, 8.34293333333333, 
8.34293333333333, 8.34293333333333), sd1 = c(0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499, 0.680168162213499, 0.680168162213499, 
0.680168162213499, 0.680168162213499), min1 = c(7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983, 7.66276517111983, 7.66276517111983, 
7.66276517111983, 7.66276517111983), max1 = c(9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683, 9.02310149554683, 9.02310149554683, 
9.02310149554683, 9.02310149554683)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -75L))

編碼:

compBtwLeftParties%>%
  filter(!is.na(competition))%>%
ggplot(aes(date, competition, shape=party, colour=party)) + 
  geom_line() + geom_point() + theme_hc() +
  scale_colour_grey(name="", labels=c("Podemos-PCE/IU", "Podemos-PSOE", "PSOE-PCE/IU"), start = .0, end = .0, aesthetics = c("colour"))  +
  geom_ribbon(aes(ymin=min1, ymax=max1, x=date), fill = "grey70",alpha=.1, inherit.aes=FALSE) +
    labs(title="Gr. 9: Wettbewerb Linksparteien") + 
    theme(text=element_text(size=12, family = "Times New Roman"),
        title=element_text(size=12),
        legend.position="bottom", 
        legend.margin = margin(t = -.3, r = 0, b = 0, l = 0, unit = "cm"),
        legend.spacing.y = unit(.1, "cm"),
        legend.background= element_rect(fill="transparent",colour=NA),
        legend.text = element_text(size = 12),
        legend.title = element_blank(), 
        legend.key.size = unit(.3, "cm"),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text=element_text(size=12)) 

先感謝您!

解決方案是添加以下代碼行: scale_shape_manual(name="", labels=c("Podemos-PCE/IU", "Podemos-PSOE", "PSOE-PCE/IU"), values=0:2) ,這使得形狀的圖例與顏色的圖例相同(在本例中為 scale_colour_grey())。

compBtwLeftParties%>%
  filter(!is.na(competition))%>%
ggplot(aes(date, competition, shape=party, colour=party)) + 
  geom_line() + 
  geom_point() + 
  theme_hc() +
  scale_colour_grey(name="", labels=c("Podemos-PCE/IU", "Podemos-PSOE", "PSOE-PCE/IU"), start=.0, end=.0)  +
  scale_shape_manual(name="", labels=c("Podemos-PCE/IU", "Podemos-PSOE", "PSOE-PCE/IU"), values=0:2) +
  geom_ribbon(aes(ymin=min1, ymax=max1, x=date), fill = "grey70",alpha=.1, inherit.aes=FALSE) +
    labs(title="Gr. 9: Wettbewerb Linksparteien") + 
    theme(text=element_text(size=12, family = "Times New Roman"),
        title=element_text(size=12),
        legend.position="bottom", 
        legend.margin = margin(t = -.3, r = 0, b = 0, l = 0, unit = "cm"),
        legend.spacing.y = unit(.1, "cm"),
        legend.background= element_rect(fill="transparent",colour=NA),
        legend.text = element_text(size = 12),
        legend.title = element_blank(), 
        legend.key.size = unit(.3, "cm"),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text=element_text(size=12)) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM