假设我用下面的数据生成了这个图(抱歉数据样本太长了,但是需要重新生成)
已经用这个代码做出来了
ggplot(q, aes(x = digest_method, y = diff,
color = forcats::fct_rev(speclib), fill = forcats::fct_rev(speclib))) +
geom_bar(stat = "identity", position = "stack") +
geom_text(aes(y = protein_total,
label = protein_total),
vjust = rep(c(1.5, -0.5), 6),
show.legend = FALSE) +
facet_wrap(~ frak,
nrow = 1)
有12个单独的geom_bar()
,它们当前由包含两个可能因子的填充着色(我的意思是,6个geom_bar()
与其他6个geom_bar()
堆叠。
**问题:*是否可以手动更改 * 每12geom_bar()
的颜色?
就像
数据
q <- structure(list(digest_method = c("Direct", "Direct", "Direct",
"Direct", "In solution", "In solution", "In solution", "In solution",
"PAC", "PAC", "PAC", "PAC"), protein_total = c(780L, 1173L, 948L,
1526L, 891L, 1444L, 1442L, 1880L, 947L, 1704L, 1598L, 2433L),
speclib = c("No", "SpecLib", "No", "SpecLib", "No", "SpecLib",
"No", "SpecLib", "No", "SpecLib", "No", "SpecLib"), frak = structure(c(1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), levels = c("Raw",
"20K Vesicles", "120K Vesicles", "Vesicles depleted"), class = "factor"),
diff = c(780L, 393L, 948L, 578L, 891L, 553L, 1442L, 438L,
947L, 757L, 1598L, 835L)), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -12L), groups = structure(list(
frak = structure(c(1L, 1L, 1L, 2L, 2L, 2L), levels = c("Raw",
"20K Vesicles", "120K Vesicles", "Vesicles depleted"), class = "factor"),
digest_method = c("Direct", "In solution", "PAC", "Direct",
"In solution", "PAC"), .rows = structure(list(1:2, 5:6, 9:10,
3:4, 7:8, 11:12), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE))
2条答案
按热度按时间3phpmpom1#
要手动或单独为条形图着色,例如
paste(speclib, digest_method, frak, sep = ".")
或fill
和/或color
上的三个变量的相互作用,并且另外将forcats::fct_rev(speclib)
Map到group
AES上。之后,它是由你来定义你的调色板。在下面的代码中,我使用六种基本颜色的阴影来创建12种颜色。此外,我添加了一个假的
geom_col
来获得一个alpha
图例,以显示哪个speclib
类别指的是较亮/较暗的阴影。zsbz8rwp2#
另一种选择是:
在这里,我们创建一个包含12个值的helper列,然后将随机生成的颜色分配给填充美学: