My current plot looks like this: But I need the bar colours to change depending on the facet_wrap group they belong to. Put simply, the bars in the 'Colour' group need to show in different shades of colour while the 'Grey' group need to show in different shades of grey. Desired output: I understand this will probably double up the items in the legend, and this is fine. I also don't mind if legend items need to be more descriptive (e.g. R.Group.Colour, R.Group.Grey, etc.). Any help would be greatly appreciated!
Data:
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"))
CodePudding user response:
You can simply combine the group
and mode
columns into a new column to use for fill
in aes()
:
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")
)