我想制作一个堆积的条形图,数据不变。我的意思是,我已经计算了绘制的百分比。根据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()
另一方面,如果我使用选项" stat_identity"数据保持不变(将两个图像的百分比与样本数据进行比较),但条形图不再堆叠。
" geom_col"选项不起作用或我做错了什么?我应该使用另一种情节方法吗?任何帮助表示赞赏。
dput:
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))
答案 0 :(得分:4)
鉴于您和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()
我使用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()