chzhxli 2019-11-29 13:41 采纳率: 0%
浏览 1655

ggplot如何调整y轴的标志顺序

图片说明
原代码是:
ggplot(GO,aes(GO_Category,GO_term))+ geom_point(aes(size=Count,color=pvalue,shape=GO_Category, group=GO_Category)) +scale_color_gradient(low="blue",high="red")+labs(color="Pvalue",size="ProteinNumber", shape="GO Category", x="GO Category",y="GO description",title="GO enrichment")+scale_x_discrete(breaks=c("Biological process","Cellular component","Molecular function"),labels= c("Biological\nprocess","Cellular\ncomponent","Molecular\nfunction"))+ theme_bw()
默认的y轴的GO term是按照首字母进行排序的,我需要把y轴的GO term分别按照GO Category集中起来,并最好能按照气泡大小排一下序,请教各位,该如何调整?

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-29 07:23
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    要调整y轴上的标记顺序,可以使用facet_wrap()函数来根据特定的变量(例如GO_category)分组并创建子图。然后,您可以使用scale_y_discrete()scale_fill_manual()来定制每个子图的颜色和填充。

    以下是调整后的代码:

    library(ggplot2)
    
    # 创建数据框
    data <- data.frame(
      GO = c(rep(c("small GTPase mediated signal transduction", "ProteinNumber", "response to alkaloid-", "protein glycosylation", "protein ADP-ribosylation", "organic substance metabolic process", "one-carbon metabolic process", "oligosaccharyl transferase activity", "nuclear outer membrane-endoplasmic reticulum membrane network-", "mitochondrial matrix")), 
               rep(NA, 8)),
      pvalue = c(0.04, 0.03, 0.02, 0.01, 0.01, 0.02, 0.03, 0.04, 0.05),
      protein_number = c(10, 20, 30, 40, 50, NA, 60, 70, 80),
      GO_category = c(rep("Biological process", 4), rep("Cellular component", 4), rep("Molecular function", 4))
    )
    
    # 绘制图形
    ggplot(data, aes(x = GO_category, y = pvalue)) +
      geom_point() +
      scale_color_manual(name = "", values = c("blue", "red")) +
      labs(title = "GO enrichment") +
      facet_wrap(~GO_category, scales = "free_y", ncol = 4) +
      theme_bw()
    

    在这个例子中,我们首先创建了一个包含不同组合的数据框,然后使用geom_point()来绘制点状图。接着,我们使用scale_color_manual()来设置颜色映射为蓝色和红色。最后,我们使用facet_wrap()函数将图形按GO_category分组,并使用theme_bw()来应用基本主题。

    请注意,这里的网格布局是通过facet_wrap()函数自动处理的,不需要额外的代码。此外,为了在不同的子图上显示不同的值,我们需要确保pvalue列中的值是相同的。如果您有多个pvalue值并且希望它们在不同子图中具有不同的大小,则可能需要进一步修改代码以适应您的需求。

    评论

报告相同问题?