我當前的情節如下所示:
但我需要根據它們所屬的 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"))
uj5u.com熱心網友回復:
您可以簡單地將group和mode列組合成一個新列以用于fillin 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")
)

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335782.html
下一篇:平行坐標圖中每個級別的單獨y軸
