【问题标题】:ggplot2 - How can I have two colour schemes when using facet_wrap?ggplot2 - 使用 facet_wrap 时如何有两种配色方案?
【发布时间】:2021-12-10 08:46:28
【问题描述】:

我目前的情节是这样的: 但是我需要根据它们所属的 facet_wrap 组来更改条形颜色。简而言之,“颜色”组中的条需要以不同的颜色显示,而“灰色”组需要以不同的灰色显示。期望的输出: 我知道这可能会使图例中的项目翻倍,这很好。我也不介意图例项目是否需要更具描述性(例如 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))

ggplot:

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

【问题讨论】:

  • 也许看看包 ggnewscale?它允许多种颜色比例,但我不确定它是否适用于这样的方面。
  • 您可以将 R.Colour.Group 和 R.BW.Group 都放在一个比例中。您需要在绘图之前操作数据(dplyr::mutate 会对它进行排序)。或者您可以创建两个图并将它们与 cowplot ggarrange 等合并在一起。

标签: r ggplot2 tidyverse visualization facet-wrap


【解决方案1】:

您可以简单地将groupmode 列组合成一个新列,用于aes() 中的fill

library(dplyr)
library(ggplot2)
df <- df %>% mutate(Group = paste(group, mode, sep = "."))

scale_fill_palette <- c(
    "R.Group.Grey"   = "#202124",
    "F.Group.Grey"   = "#808083",
    "G.Group.Grey"   = "#C0C0C0",
    "R.Group.Colour" = "#e69f00",
    "F.Group.Colour" = "#56b4e9",
    "G.Group.Colour" = "#009e73"
)
df.plot <-
    ggplot(df, aes(x = stim, y = emmean, fill = Group)) +
    scale_fill_manual("legend", values = scale_fill_palette) + 
    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")
    )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多