【问题标题】:ggplot2 bar chart in order of fill group按填充组顺序排列的ggplot2条形图
【发布时间】:2020-11-02 03:32:46
【问题描述】:

我有一个如下所示的数据集:

第一列包含所有唯一值,而第三列包含 3 个因子。我想创建一个像这样的条形图:

我一直试图在 ggplot 中得到它并且已经完成了

ggplot(data, aes(as.factor(GO), pvalue, fill = Process)) + 
  geom_bar(position = "dodge", width = 0.5, stat = "identity")

但到目前为止,我的情节是这样的:

我该怎么做才能按组按顺序排列条形?

提前致谢!

这是一个示例数据:

GO <- c("T cell chemotaxis (GO:0010818)", "T cell chemotaxis (GO:0010818)",
            "eosinophil chemotaxis (GO:0048245)", "CXCR3 chemokine receptor binding (GO:0048248)",
            'hemokine activity (GO:0008009)', 'CXCR chemokine receptor binding (GO:0045236)',"cytoplasmic vesicle lumen (GO:0060205)",
            "vesicle lumen (GO:0031983)", "collagen-containing extracellular matrix (GO:0062023)
    ")
    
pvalue <- c(2.49e-02, 3.07e-08, 1.90e-06, 2.22e-08, 1.72e-08, 1.63e-08, 6.18e-03, 3.87e-08
, 3.19e-07)
Process <- as.factor(rep(c("BP", "CP", "MP"),c(3,3,3)))
data <- data.frame(GO, pvalue,process)

【问题讨论】:

  • 这能回答你的问题吗? Order Bars in ggplot2 bar graph
  • @semaphorism 不,它没有,我试过了,但我的酒吧仍然有问题。我做了: theTable
  • 请以可用的格式发布您的数据示例,以便我们帮助您进行故障排除。或者,我建议您查看wego.genomics.cn,这可能会为您节省大量时间/精力。
  • @jared_mamrot 感谢您的建议!我添加了一小部分数据。我需要把它变成的人想要它专门绘制在 R

标签: r ggplot2 plot bar-chart


【解决方案1】:

这能解决您的问题吗?

GO <- c("T cell chemotaxis (GO:0010818)", "T cell chemotaxis (GO:0010818)",
        "eosinophil chemotaxis (GO:0048245)", "CXCR3 chemokine receptor binding (GO:0048248)",
        'hemokine activity (GO:0008009)', 'CXCR chemokine receptor binding (GO:0045236)',"cytoplasmic vesicle lumen (GO:0060205)",
        "vesicle lumen (GO:0031983)", "collagen-containing extracellular matrix (GO:0062023)
    ")

pvalue <- c(2.49e-02, 3.07e-08, 1.90e-06, 2.22e-08, 1.72e-08, 1.63e-08, 6.18e-03, 3.87e-08
            , 3.19e-07)
Process <- as.factor(rep(c("BP", "CP", "MP"),c(3,3,3)))
data <- data.frame(GO, pvalue, Process)
data$GO <- factor(data$GO, levels=unique(GO[order(Process,GO)]), ordered=TRUE)
ggplot(data, aes(x = GO, y = -log(pvalue), fill = Process)) +
  geom_col(position = "dodge", width = 0.5) +
  theme_bw() +
  coord_flip()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    相关资源
    最近更新 更多