gpt4 book ai didi

r - 数据不变的堆叠叠加条

转载 作者:行者123 更新时间:2023-12-04 10:30:11 24 4
gpt4 key购买 nike

我想制作一个数据不变的堆叠条形图。我的意思是,我已经计算出要绘制的百分比。根据 ggplot2 手册“geom_col 使用 stat_identity:它按原样保留数据”。但是,看起来它不起作用,因为绘图的百分比与样本数据的百分比不同。

here 下载示例数据.

代码如下:

ggplot(data=df, aes(x = Pathway, y = value, fill = variable)) +
scale_fill_manual(values=c("#005588", "#E69F00")) +
#stat_identity(geom="bar", width=0.5) +
geom_col(width=0.5) +
#geom_bar(stat="identity", width=0.5) +
facet_grid(. ~ Timepoint) +
coord_flip() +
theme_bw()

geom_col changes data and rows order

另一方面,如果我使用“stat_identity”选项,数据保持不变(将两个图像的百分比与样本数据进行比较),但条形图不再堆叠。

stat_identity do not touch the data but looses stacked bars.

是“geom_col”选项不起作用还是我做错了什么?我应该使用另一种绘图方法吗?任何帮助表示赞赏。

输入:

structure(list(Pathway = c("Antigen Presentation Pathway", "Graft-versus-  Host Disease Signaling", 
"T Helper Cell Differentiation", "Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells",
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway",
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation",
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells",
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway",
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation",
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells",
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway",
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation",
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells",
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway",
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation",
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells",
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway",
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation",
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells",
"Communication between Innate and Adaptive Immune Cells"), Timepoint = c("15DPI",
"15DPI", "15DPI", "15DPI", "15DPI", "30DPI", "30DPI", "30DPI",
"30DPI", "30DPI", "45DPI", "45DPI", "45DPI", "45DPI", "45DPI",
"15DPI", "15DPI", "15DPI", "15DPI", "15DPI", "30DPI", "30DPI",
"30DPI", "30DPI", "30DPI", "45DPI", "45DPI", "45DPI", "45DPI",
"45DPI"), variable = c("Targets", "Targets", "Targets", "Targets",
"Targets", "Targets", "Targets", "Targets", "Targets", "Targets",
"Targets", "Targets", "Targets", "Targets", "Targets", "DEGs",
"DEGs", "DEGs", "DEGs", "DEGs", "DEGs", "DEGs", "DEGs", "DEGs",
"DEGs", "DEGs", "DEGs", "DEGs", "DEGs", "DEGs"), value = c(2.63157894736842,
4.16666666666667, 1.36986301369863, 3.125, 1.12359550561798,
7.89473684210526, 18.75, 8.21917808219178, 18.75, 7.86516853932584,
15.7894736842105, 16.6666666666667, 10.958904109589, 9.375, 8.98876404494382,
44.7368421052632, 35.4166666666667, 43.8356164383562, 37.5, 31.4606741573034,
47.3684210526316, 43.75, 42.4657534246575, 37.5, 33.7078651685393,
52.6315789473684, 39.5833333333333, 39.7260273972603, 31.25, 31.4606741573034)), .Names = c("Pathway", "Timepoint", "variable",
"value"), class = "data.frame", row.names = c(NA, -30L))

最佳答案

鉴于您和 Gregor 在上面的评论中的讨论,听起来您不希望这些图相互堆叠,而是重叠。我相信这对您有用:

ggplot(data=df, aes(x = Pathway, y = value, fill = variable)) +
scale_fill_manual(values=c("#005588", "#E69F00")) +
geom_col(width = 0.5, alpha = 0.5, position = "identity") +
facet_grid(. ~ Timepoint) +
coord_flip() +
theme_bw()

enter image description here

我使用 position = "identity" 来确保条形图不会堆叠。我还必须使用 alpha = 0.5 使条形透明,以便您可以看到它们。


如果你想让它们并排而不是堆叠,另一种选择是使用 position = "dodge":

ggplot(data=df, aes(x = Pathway, y = value, fill = variable)) +
scale_fill_manual(values=c("#005588", "#E69F00")) +
geom_col(width=0.5, position = "dodge") +
facet_grid(. ~ Timepoint) +
coord_flip() +
theme_bw()

enter image description here

关于r - 数据不变的堆叠叠加条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43989191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com