【问题标题】:Changing colours of grouped bar chart in ggplot2在ggplot2中更改分组条形图的颜色
【发布时间】:2018-11-30 21:54:25
【问题描述】:

对于示例数据框:

df <- structure(list(year = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 
3, 3, 4, 4, 4, 4, 4), imd.quintile = c(1, 2, 3, 4, 5, 1, 2, 3, 
4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5), average_antibiotic = c(1.17153515458827, 
1.11592565388857, 1.09288449967773, 1.07442652168281, 1.06102887394413, 
1.0560582933182, 1.00678980505929, 0.992997489072538, 0.978343676071694, 
0.967900478870214, 1.02854157116164, 0.98339099101476, 0.981198852494798, 
0.971392872980818, 0.962289579742817, 1.00601488964457, 0.951187417739673, 
0.950706064156994, 0.939174499710836, 0.934948233015044)), .Names = c("year", 
"imd.quintile", "average_antibiotic"), row.names = c(NA, -20L
), vars = "year", drop = TRUE, class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"))

我正在制作一张图表,详细说明按 imd.decile BY year 开具抗生素处方的差异:

ggplot(plot_data.quintiles) + 
  geom_col(aes(x = year, y = average_antibiotic, group=imd.quintile, fill=imd.quintile), position = "dodge") +
             ylab("Antibiotic STAR-PU") +
  xlab("Year") +
  theme_bw() +
  ylim(0, 1.5)+
  scale_colour_brewer("clarity")

蓝色的选择不合我的口味,因为 imd.quintiles 之间的差异不是很明显。我已经阅读了各种帖子,hereherehere,但似乎都没有回答我的问题。

我尝试使用“清晰”颜色来获得更广泛的颜色选择。如何正确更改 ggplot2 图中的填充颜色?我有什么选择?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这是你想要的吗?使用factor(imd.quintile) 创建离散(分类)数据,否则ggplot 会将数字/整数imd.quintile 视为连续数据。

    df <- data.frame(
                     year = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4,
                              4, 4),
             imd.quintile = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3,
                              4, 5),
       average_antibiotic = c(1.17153515458827, 1.11592565388857, 1.09288449967773,
                              1.07442652168281, 1.06102887394413, 1.0560582933182,
                              1.00678980505929, 0.992997489072538, 0.978343676071694,
                              0.967900478870214, 1.02854157116164, 0.98339099101476,
                              0.981198852494798, 0.971392872980818,
                              0.962289579742817, 1.00601488964457, 0.951187417739673,
                              0.950706064156994, 0.939174499710836, 0.934948233015044)
    )
    
    library(ggplot2)
    p1 <- ggplot(df) +
      geom_col(aes(
        x = year, y = average_antibiotic,
        group = imd.quintile, fill = factor(imd.quintile)), position = "dodge") +
      ylab("Antibiotic STAR-PU") +
      xlab("Year") +
      theme_bw() +
      ylim(0, 1.5)
    
    p1 +
      scale_fill_brewer(palette = "Set2") # use scale_fill_xxx to chose the desired color palette
    

    如果您更喜欢连续(顺序)颜色图,viridisscico 是不错的选择:

    p1 +
      scale_fill_viridis_c(option = 'E', direction = -1)
    

    # install.packages('scico')
    library(scico)
    p1 +
      scale_fill_scico()
    

    reprex package (v0.2.1.9000) 于 2018 年 11 月 29 日创建

    【讨论】:

      【解决方案2】:

      scale_####_brewer 使用来自RColorBrewer 的调色板,没有名为“Clarity”的调色板。

      使用RColorBrewer::display.brewer.all() 查看可用的调色板,然后使用palette 参数按名称调用它们。您还需要将imd.quintile 变量更改为字符或因子。您还通过填充而不是颜色来映射您的美学,因此您需要使用scale_fill_brewer

      ggplot(df) + 
        geom_col(aes(x = year, y = average_antibiotic, group=imd.quintile, fill=imd.quintile), position = "dodge") +
        ylab("Antibiotic STAR-PU") +
        xlab("Year") +
        theme_bw() +
        ylim(0, 1.5) + 
        scale_fill_brewer(palette = "Spectral")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-07
        • 1970-01-01
        • 2014-08-06
        • 2018-04-02
        • 2021-09-17
        • 2016-03-06
        • 1970-01-01
        相关资源
        最近更新 更多