在ggplot2堆积条形图

时间:2017-11-17 16:18:53

标签: r ggplot2

我正在进行演示,并希望通过保留此特定因子水平的颜色来突出显示我在幻灯片上的一个因子水平的发生率,同时使其他变量的其他因子水平变灰。 / p>

enter image description here

使用Arthritis库中的vcd数据集作为示例:

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

例如,当我告诉观众关于男性的明显改善的情况时,我是否可以获得一个版本的情节,该版本保留了男性明显改善的颜色,但灰显了其他一切?最好是通过使用不同的灰色阴影来保持边界?

4 个答案:

答案 0 :(得分:2)

考虑这个例子:

library(scales)
library(ggplot2)

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

enter image description here

要更改指定的字段,而其他字段保留默认的ggplot颜色:

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

enter image description here

或者用阴影将其余部分变灰

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")

enter image description here

保持图例只是在初始图层上绘制上述内容:

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")

enter image description here

答案 1 :(得分:1)

添加

object

完整scale_fill_manual(values=c("Marked" = "RoyalBlue", "Some" = "DarkGrey", "None"="LightGrey"))来电:

ggplot

enter image description here

答案 2 :(得分:1)

另一个选择是替换我们想要由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"))
    

    enter image description here

  • 突出显示男性标记

    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"))
    

    enter image description here

  • 答案 3 :(得分:0)

    您可以添加包含颜色地图的列,并将其与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"'))