英文:
How to apply separate colors for multiple geom_bars that is based on three different x-axis values and filled with two different factors
问题
以下是您要翻译的内容:
这是使用下面的数据生成的绘图(为了复制,抱歉提供了较长的数据示例)
它是用以下代码创建的
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()
堆叠在一起)。
问题:
我能手动更改每个12个geom_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))
英文:
Let's say I have this plot generated with the data below (apologies for the long data sample, but it was needed to reproduce)
It has been made with this code
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)
There are 12 individual geom_bar()
, which are currently colored by a fill that contains two possible factors (I mean, 6 geom_bar()
stacked with 6 other geom_bar()
.
Question:
Can I manually change the color of each 12 geom_bar()
?
Something like
Data
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))
答案1
得分: 4
手动为柱形图上色或将每个条形图映射到特定值,例如 paste(speclib, digest_method, frak, sep = ".")
或对 fill
和/或 color
上的三个变量进行交互映射,并额外映射 forcats::fct_rev(speclib)
到 group
aes。随后您可以定义自己的调色板。在下面的代码中,我使用了六个基本颜色的不同色调来创建12种颜色。此外,我添加了一个虚拟的 geom_col
来获得一个 alpha
图例,以显示哪个 speclib
类别对应较浅/较深的色调。
英文:
To color your bars manually or individually map e.g. paste(speclib, digest_method, frak, sep = ".")
or the interaction of the three variables on fill
and/or color
and additionally map forcats::fct_rev(speclib)
on the group
aes.
Afterwards it's up to you to define your color palette. In the code below I use shades of six base colors to create 12 colors. Additionally I added a fake geom_col
to get an alpha
legend to show which speclib
category refers to the lighter/darker shade.
library(ggplot2)
pal <- scales::brewer_pal(type = "qual")(6)
pal <- c(colorspace::darken(pal, .4), colorspace::lighten(pal, .4))
ggplot(q, aes(
x = digest_method, y = diff,
color = paste(speclib, digest_method, frak, sep = "."),
group = forcats::fct_rev(speclib),
fill = paste(speclib, digest_method, frak, sep = "."),
)) +
geom_col(
aes(alpha = forcats::fct_rev(speclib)),
fill = "transparent"
) +
geom_col() +
geom_text(
aes(
y = diff,
label = protein_total
),
color = "black",
vjust = 1.5,
position = position_stack(),
show.legend = FALSE
) +
scale_fill_manual(
values = pal, aesthetics = c("color", "fill"),
guide = "none"
) +
facet_wrap(~frak,
nrow = 1
) +
guides(alpha = guide_legend(
override.aes = list(
alpha = 1,
fill = c(
colorspace::lighten("grey", .4),
colorspace::darken("grey", .4)
)
)
))
答案2
得分: 2
以下是代码中的翻译部分:
另一种选项是:
在这里,我们创建了一个带有12个值的辅助列,并将随机生成的颜色分配给填充美学:
library(ggplot2)
random_colors <- sample(colors(), 12)
q %>%
ungroup() %>%
mutate(helper = row_number()) %>%
ggplot(aes(x = digest_method, y = diff,
fill = factor(helper))) +
geom_bar(stat = "identity", position = position_stack(reverse = TRUE)) +
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) +
scale_fill_manual(values = random_colors) +
theme(legend.position = "none")
英文:
Another option is:
Here we create a helper column with 12 values and assign then randomly generated colors to the fill aesthetics:
library(ggplot2)
random_colors <- sample(colors(), 12)
q %>%
ungroup() %>%
mutate(helper = row_number()) %>%
ggplot(aes(x = digest_method, y = diff,
fill = factor(helper))) +
geom_bar(stat = "identity", position = position_stack(reverse = TRUE)) +
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) +
scale_fill_manual(values = random_colors) +
theme(legend.position = "none")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论