代码之家  ›  专栏  ›  技术社区  ›  Benjamin Telkamp

更改分面条形图中条形的颜色

  •  1
  • Benjamin Telkamp  · 技术社区  · 7 年前

    我试图改变以下情节中的一些颜色:

    下面的代码给了我以下情节,已经相当漂亮了:

    enter image description here

    我仍然希望更改绘图中4个条的颜色,例如“green4”、“darkgreen”、“orangered3”和“red3”,但不更改绘图中的任何其他内容(例如图例中使用的标签(当然,图例中的颜色也应该更改))。我的数据(df)有三个变量。变量“V1n”只是因子变量V1的数字版本。这只是最后一点我不能左右我的头,有人知道如何改变默认使用的四种颜色吗?

    library(ggplot2)
    library(dplyr)
    glimpse(df)
    
    # Observations: 300
    # Variables: 3
    # $ METING.f <fctr> Meting 0, Meting 0, Meting 0, Meting 0, Meting 0,...
    # $ V1n      <int> 2, 1, 2, 2, 2, 2, 2, 1, 2, 1, 2, 1, 1, 2, 2, 2, 1,...
    # $ V1       <fctr> Ja, meerdere, namelijk..., Ja, Ja, meerdere, name...  
    
    
    ggplot(df, aes(x = V1n, fill = V1)) + 
      geom_bar(aes(y = (..count..)/tapply(..count..,..PANEL..,sum)[..PANEL..])) + 
      scale_y_continuous(labels = scales::percent) + 
      geom_text(aes(y = ((..count..)/sum(..count..)), 
                    label = scales::percent((..count..)/tapply(..count..,..PANEL..,sum)[..PANEL..])), 
                    stat = "count", vjust = -0.25) +
      facet_grid(.~ METING.f) +
      scale_fill_discrete(name = "Beschikt uw instelling over een\naandachtsfunctionaris kindermishandeling?\n") +
      labs(title = " ",
           x = "Vraag 1",
           y = "Percentage")  +
      theme(axis.text.x = element_blank(),
            axis.ticks.x=element_blank())
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   pogibas    7 年前

    scale_fill_manual .

    scale_fill_manual(name = "Beschikt...",
                      values = c("green4", "darkgreen", "orangered3", "red3"))
    

    brewer pallets 具有 scale_fill_brewer

    注:如果您不想设置填充名称 你可以设置它 labs() (正如您为 title x ). 例如 labs(fill = "yourFillName") .