R ggplot根据特定输入在图形上显示文本

时间:2018-12-13 09:32:06

标签: r ggplot2 shiny

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

我现在正在尝试在图形上显示注释,以便用户选择高富集基因的特定列表中的基因时,图形上将在对应于该细胞类型的列上方显示符号或文本。告诉他们。第二个基因列表作为单独的文件上传。

到目前为止,我已经通过在ggplot中使用注释功能并把x值作为CellType来使该注释显示在该特定单元格上方,从而使该方法起作用。当选择该列表中的一个基因时,它可以很好地显示文本。以下是一些模拟数据,然后是精简版的代码以制作图形。

    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") 
      })

问题是当选择不在该列表中的基因时。如果GeneEnriched文件仅包含丰富基因的列表,那么在选择不在列表中的基因时出现错误。如果GeneEnriched包含所有基因,但如果基因被富集,则仅在第2列中具有Celltype名称,则它将插入另一列并弄乱图形的顺序。我尝试过的两种类型的GeneEnriched表的示例如下。

GeneEnriched类型1:

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

GeneEnriched类型2:

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

如何获取它,以便在选择次要列表中的基因时出现文本,但是当选择不在此次要列表中的基因时什么也不会发生。

0 个答案:

没有答案