简体   繁体   English

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

[英]Changing colours of grouped bar chart in ggplot2

For a sample dataframe: 对于示例数据框:

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

I am producing a graph detailing the differences in antibiotic prescribing BY 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")

The blue colour choice isn't to my taste, as the differences between the imd.quintiles isn't very distinctive. 蓝色的选择并不符合我的口味,因为imd.quintiles之间的差异不是很明显。 I have read various posts, here , here and here , but none of which seem to answer my question. 我已经在这里这里这里阅读了各种帖子,但似乎都没有一个回答我的问题。

I attempted to use the 'clarity' colours to get a wider range of colour choices. 我试图使用“清晰度”颜色来获得更广泛的颜色选择。 How can I correctly change the fill colour in my ggplot2 graph? 如何正确更改ggplot2图形中的填充颜色? what options do I have? 我有什么选择?

Is this what you want? 这是你想要的吗? Use factor(imd.quintile) to create discrete (categorical) data otherwise ggplot will treat numeric/integer imd.quintile as continuous. 使用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

If you prefer continuous (sequential) colormaps, viridis or scico are good options: 如果您喜欢连续(顺序)色图,则viridisscico是不错的选择:

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

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

Created on 2018-11-29 by the reprex package (v0.2.1.9000) reprex软件包 (v0.2.1.9000)创建于2018-11-29

scale_####_brewer uses palettes from RColorBrewer , there's no palette called "Clarity". scale_####_brewer使用RColorBrewer中的调色板,没有名为“ Clarity”的调色板。

Use RColorBrewer::display.brewer.all() to see what palette's are available, then call them by name with the palette arg. 使用RColorBrewer::display.brewer.all()查看可用的调色板,然后使用palette arg通过名称进行调用。 Also you need to change the imd.quintile variable to be either character or factor. 另外,您需要将imd.quintile变量更改为字符或因子。 You're mapping your aesthetics by fill also, not colour, so you need to use scale_fill_brewer . 您也要通过填充而不是颜色来映射美学,因此需要使用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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM