ggplot2使用facet_wrap分面时如何设置两套独立填充配色方案?
问题描述
我当前绘制的图表效果如下:
我需要让柱状图的填充色随facet_wrap的分组变化:简单来说,Colour分面的柱状图使用不同饱和度的彩色填充,Grey分面的柱状图使用不同灰度的灰色填充,期望效果如下:
我可以接受图例条目翻倍,也接受图例命名更具象化(例如R.Group.Colour、R.Group.Grey等),恳请大家提供解决方案。
示例数据
df <- structure(list(group = c("R.Group", "R.Group", "R.Group", "R.Group", "R.Group", "R.Group", "F.Group", "F.Group", "F.Group", "F.Group", "F.Group", "F.Group", "G.Group", "G.Group", "G.Group", "G.Group", "G.Group", "G.Group"), mode = c("Grey", "Grey", "Grey", "Colour", "Colour", "Colour", "Grey", "Grey", "Grey", "Colour", "Colour", "Colour", "Grey", "Grey", "Grey", "Colour", "Colour", "Colour" ), stim = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Words", "Drawings", "Photographs"), class = "factor"), emmean = c(0.100851416515973, 0.108649789029536, 0.0664556962025316, 0.0411392405063291, 0.0574894514767932, 0.0274261603375527, 0.378405666063894, 0.23372513562387, 0.139240506329114, 0.185126582278481, 0.209915611814346, 0.0611814345991561, 0.185299879445449, 0.100663050030139, 0.110759493670886, 0.147151898734177, 0.0743670886075949, 0.0569620253164557), ymin = c(0.0807866262301694, 0.0848024457881558, 0.0468323222930633, 0.0269666440471827, 0.0402160043121709, 0.015560956877645, 0.344334779798359, 0.202260415849806, 0.11272781055522, 0.155246316858577, 0.178339882849687, 0.0428311209142308, 0.159640657907614, 0.0791229394712789, 0.0867398048483564, 0.119923200384746, 0.0551207397946581, 0.0390178805506117 ), ymax = c(0.120916206801778, 0.132497132270916, 0.0860790701119999, 0.0553118369654756, 0.0747628986414156, 0.0392913637974605, 0.412476552329429, 0.265189855397934, 0.165753202103008, 0.215006847698385, 0.241491340779005, 0.0795317482840815, 0.210959100983284, 0.122203160588998, 0.134779182493416, 0.174380597083609, 0.0936134374205317, 0.0749061700822997)), class = "data.frame", row.names = c(NA, -18L))
当前绘图代码
df.plot <- ggplot(df, aes(x = stim, y = emmean, fill = group)) + scale_fill_manual("legend", values = c("R.Group" = "#202124", "F.Group" = "#808083","G.Group" = "#C0C0C0")) + geom_col(position = position_dodge()) + geom_errorbar(aes(ymin = ymin, ymax = ymax), width=0.2, size=0.3, position = position_dodge(0.9)) + facet_wrap(~mode, strip.position = "bottom") + scale_y_continuous(limits = c(0, 0.5)) + theme_classic() + theme(axis.title.x = element_blank(), axis.text.x = element_text(size = 9), axis.text.y = element_text(size = 9), strip.placement = "outside", strip.background = element_rect(fill = "white", size = 0.5, linetype = "solid", colour = "black"), strip.text = element_text(size = 9), plot.margin = margin(t = 1, r = 1, b = 0, l = 1, "lines"))
解决方案
核心思路是构造一个同时包含分组和分面模式的新变量作为填充映射项,就可以为不同分面的同分组设置完全独立的颜色。你可以直接复用以下修改后的代码:
首先给数据集新增组合变量:
# 新增组合变量,把group和mode拼接为填充映射的维度 df$fill_group <- paste(df$group, df$mode, sep = ".")
然后修改绘图代码:
library(ggplot2) df.plot <- ggplot(df, aes(x = stim, y = emmean, fill = fill_group)) + # 自定义颜色,前三个为灰度分面的颜色,后三个为彩色分面的颜色 scale_fill_manual("分组", values = c("R.Group.Grey" = "#202124", "F.Group.Grey" = "#808083", "G.Group.Grey" = "#C0C0C0", "R.Group.Colour" = "#D92121", # 红色对应R组彩色 "F.Group.Colour" = "#2176D9", # 蓝色对应F组彩色 "G.Group.Colour" = "#21D957"), # 绿色对应G组彩色 # 按需修改标签命名即可 labels = c("R.Group.Grey" = "R组(灰度)", "F.Group.Grey" = "F组(灰度)", "G.Group.Grey" = "G组(灰度)", "R.Group.Colour" = "R组(彩色)", "F.Group.Colour" = "F组(彩色)", "G.Group.Colour" = "G组(彩色)")) + geom_col(position = position_dodge(0.9)) + geom_errorbar(aes(ymin = ymin, ymax = ymax), width=0.2, size=0.3, position = position_dodge(0.9)) + facet_wrap(~mode, strip.position = "bottom") + scale_y_continuous(limits = c(0, 0.5)) + theme_classic() + theme(axis.title.x = element_blank(), axis.text.x = element_text(size = 9), axis.text.y = element_text(size = 9), strip.placement = "outside", strip.background = element_rect(fill = "white", size = 0.5, linetype = "solid", colour = "black"), strip.text = element_text(size = 9), plot.margin = margin(t = 1, r = 1, b = 0, l = 1, "lines")) # 输出图片 print(df.plot)
彩色的色值可以根据你的需求自行调整,图例的命名也可以通过修改scale_fill_manual里的labels参数自定义。
内容的提问来源于stack exchange,提问作者adajam
相关产品推荐
相关产品推荐

