r - 更改ggplot2中分组条形图的颜色

标签 r ggplot2

对于示例数据框:

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 抗生素处方的差异:

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 之间的差异不是很明显。我读过各种帖子,here , herehere ,但似乎都没有回答我的问题。

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

最佳答案

这是你想要的吗?使用 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 创建于 2018-11-29 (v0.2.1.9000)

关于r - 更改ggplot2中分组条形图的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53543284/

相关文章:

r - 连接图标题中的字符串和表达式

r - 如何在ggplot2图例中包含黄土线?

r - ggplot2调色板图例不显示

r - Y 轴上带有百分比的累积直方图

r - 根据 "closest value"测试选择性地包含列

r - 在R中随机分割数据

r - ggplot2 boxplot中位数未按预期绘制

r - 如何绘制两组收入群体人口和值(value)观

r - 使用 magrittr tee %T>% 运算符会产生错误

r - 将辅助 x Axis 添加到 ggplot 线图,纯粹用于提供信息