ggplot2 - 如何在使用 facet_wrap 时有两种配色方案?
ggplot2 - How can I have two colour schemes when using 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))
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"))
您可以简单地将 group
和 mode
列组合成一个新列以用于 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")
)
我现在的剧情是这样的:
数据:
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"))
您可以简单地将 group
和 mode
列组合成一个新列以用于 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")
)