簡體   English   中英

在ggplot2堆疊條形圖中手動更改一個因子水平的顏色

[英]Manually change colour of one factor level in ggplot2 stacked barplot

我正在做一個演講,並希望通過保留特定因子水平的顏色,同時在其他變量中使其他因子水平變灰的同時,突出顯示幻燈片中某一因子水平的發生率。

在此處輸入圖片說明

vcd庫中的Arthritis數據集為例:

library(vcd)
ggplot(Arthritis, aes(x = Sex, fill = Improved)) + geom_bar(position = "fill")

例如,當我向聽眾介紹男性明顯改善的情況時,我是否可以保留該情節的某個版本,該版本保留男性明顯改善的顏色,但將其他所有顏色都灰顯呢? 最好通過使用不同的灰色陰影來保留邊界?

考慮以下示例:

library(scales)
library(ggplot2)

ggplot(Arthritis)+
  geom_bar(aes(x = Sex, group = Improved), fill = c("red", "blue", "green", "yellow", "grey50", "black"), position = "fill")

在此處輸入圖片說明

要更改指定的字段,而其他字段則保留默認的ggplot顏色:

ggplot(Arthritis)+
  geom_bar(aes(x = Sex,
               group = Improved),
           fill = c(rev(hue_pal()(3)),
                    "black",
                    hue_pal()(3)[2:1]),
            position = "fill")

在此處輸入圖片說明

或用陰影將其余的部分變灰

ggplot(Arthritis)+
  geom_bar(aes(x = Sex,
               group = Improved),
           fill = c(paste0("grey", 7:9*10),
                    hue_pal()(3)[3],
                    paste0("grey", 8:9*10)),
           position = "fill")

在此處輸入圖片說明

要保留圖例,只需在您的初始圖層上繪制以上內容即可:

ggplot(Arthritis)+
  geom_bar(aes(x = Sex, fill = Improved),  position = "fill")+
  geom_bar(aes(x = Sex,
               group = Improved),
           fill = c(paste0("grey", 7:9*10),
                    hue_pal()(3)[3],
                    paste0("grey", 8:9*10)),
           position = "fill")

在此處輸入圖片說明

scale_fill_manual(values=c("Marked" = "RoyalBlue", "Some" = "DarkGrey", "None"="LightGrey"))

完整的ggplot調用:

ggplot(Arthritis, aes(x = Sex, fill = Improved)) + 
  geom_bar(position = "fill")+
  scale_fill_manual(values=c("Marked" = "RoyalBlue", "Some" = "DarkGrey", "None"="LightGrey"))

在此處輸入圖片說明

另一個選擇是用NA替換我們要突出顯示的數據點,然后使用scale_fill_grey為參數na.value 我們手動重新標記圖例以顯示原始名稱。

  • 突出顯示所有已標記

     library(dplyr) ggplot(Arthritis %>% mutate(Improved = replace(Improved, Improved == "Marked", NA))) + geom_bar(position = "fill", aes(x = Sex, fill = Improved))+ scale_fill_grey(start = 0.8, end = 0.6, na.value = "RoyalBlue", labels = c("None", "Some", "Marked")) 

    在此處輸入圖片說明

  • 突出顯示男性標記

     ggplot(Arthritis %>% mutate(Improved = replace(Improved, Improved== "Marked" & Sex == "Male", NA))) + geom_bar(position = "fill", aes(x = Sex, fill = Improved))+ scale_fill_grey(start = 0.8, end = 0.2, na.value = "RoyalBlue", labels = c("None", "Some", "Marked", "Marked")) 

    在此處輸入圖片說明

  • 您可以添加一個包含顏色映射的列,並將其與scales_fill_identity()


    reprex::reprex_info()
    #> Created by the reprex package v0.1.1.9000 on 2017-11-18
    
    library(vcd)
    #> Loading required package: grid
    
    lvls <- levels(Arthritis$Improved)
    color_maps <- scales::grey_pal(start = 0.8, end = 0.9)(length(lvls))
    names(color_maps) <- lvls
    
    
    library(dplyr, warn.conflicts = FALSE)
    
    Arthritis_w_fill <- Arthritis %>%
      mutate(
        fill = if_else(
          Sex == "Male" & Improved == "Marked",
          "blue",
          color_maps[Improved]
      )
    )
    
    
    library(ggplot2)
    
    ggplot(Arthritis_w_fill, aes(x = Sex, fill = fill)) +
      geom_bar(position = "fill") +
      scale_fill_identity(guide = "legend",
                          breaks = c(color_maps, "blue"),
                          labels = c(lvls, 'Sex == "Male" & Improved == "Marked"'))
    

    暫無
    暫無

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

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