首页 > 解决方案 > R ggplot 在图形上显示文本以响应特定输入

问题描述

好的,所以我正在使用 R Shiny 制作一个网络应用程序,它允许用户从 19000 个左右的列表中选择一个基因,然后查看与该基因相关的数据和计算。到目前为止,我的图表和绘图工作正常(这里有一些用户的帮助)。

我现在正试图在我的图表上显示注释,这样当用户选择一个位于高度富集基因的特定列表中的基因时,一个符号或文本会出现在对应于细胞类型的列上方的图表上,告诉他们. 第二个基因列表作为单独的文件上传。

到目前为止,我已经通过在 ggplot 中使用 annotate 函数来完成这项工作,并将 x 值作为 CellType 以便注释出现在该特定单元格上方。and it displays text well when a gene within that list is selected. 以下是一些模拟数据,然后是用于制作图形的代码的精简版本。

    Mean_list <-
      data.frame(Cells = factor(c("Celltype1", "Celltype2", "Celltype3", "Celltype4"), 
                                     levels =c("Celltype1", "Celltype2", "Celltype3", "Celltype4")),
                 Mean = c(mean(c(1, 2, 3)), mean(c(5, 8, 4)), mean(c(9, 8 ,3)), mean(c(3, 6, 8, 5))))


    output$celltype_bar_plot <- renderPlot({
        ggplot() +
  geom_col(data = Mean_list, aes(Cells, Mean, fill = Cells)) +
        annotate("text", x = GeneEnriched[GeneEnriched$X1 %in% input$gene_select, 2] label = "enriched") 
      })

The problem is when a gene NOT in that list is selected. 如果 GeneEnriched 文件仅包含富集基因列表,则在选择不在列表中的基因时会出现错误。如果 GeneEnriched 包含所有基因,但如果基因富集,则第 2 列中只有 Celltype 名称,那么它会插入一个额外的列并打乱图表的顺序。我尝试过的 2 种 GeneEnriched 表的示例如下。

基因富集 1 型:

      X1          X2 
1   Gene1      Celltype1   
2   Gene3      Celltype3   
3   Gene4      Celltype4   
4   Gene24     Celltype3   
5   Gene52     Celltype1   
6   Gene54     Celltype2 

基因富集类型 2:

      X1          X2 
1   Gene1      Celltype1   
2   Gene2        
3   Gene3      Celltype3   
4   Gene4      Celltype4   
5   Gene5       
6   Gene6     

How can I get it so that the text appears when a gene in the secondary list is selected, but that nothing happens when a gene NOT in this secondary list is selected.

标签: rggplot2shiny

解决方案


推荐阅读