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

标签 r ggplot2 bar-chart

我想制作一个数据不变的堆叠条形图。我的意思是,我已经计算出要绘制的百分比。根据 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/

相关文章:

reporting-services - SSRS : Custom bar width between multi-level categories

r - dplyr分组依据,将值从上一个分组结转到下一个

r - 在 Rstudio 中将 R 与 Spark 连接 - 启动 Spark shell 失败。端口文件不存在

R 使用自定义函数按列处理数据的最佳方式

r - 尝试在 ggplot2 中创建多面图时出错

google-visualization - getSelection() 从 Google Chart API 中的行获取数据

R - 转换为 LaTeX 时处理表格中 [x,y] 形式的切割间隔

r - 绘制大型数据集的堆积条形图并在 r 中设置绘图的条形限制

r - 构面的总体标签

r - 将 x Axis 放置在具有负 y 值的条形图顶部