r - 如何遮盖绘图子区域并使用ggrepel标记数据点的子集?

标签 r ggplot2 bioinformatics ggrepel

我制作了这张火山图,并希望对其进行如下改进:

  • 用蓝色数据点完全遮盖了区域:使用当前代码,我无法将阴影扩展到超出您所看到的范围。我希望它能一直达到绘图区域的极限。
  • geom_text允许我标记数据点的子集,但是使用ggrepel这样做可以添加将数据点与标签连接起来的行,从而提高标签的清晰度。如何重用geom_text中的现有ggrepel代码来实现此目的?

  • 这是我的代码:
    ggplot(vol.new, aes(x = log2.fold.change, y = X.NAME., fill = Color)) + # Define data frame to be used for plotting; define data for x and y axes; crate a scatterplot object.
    
      geom_point(size = 2, shape = 21, colour = "black") + # Define data point style.
    
      ggtitle(main.title, subtitle = "Just a little subtitle") + # Define title and subtitle.
    
      labs(x = x.lab, y = y.lab) + # Define labels for x and y axes.
    
      scale_x_continuous(limits = c(-3, 3), breaks = seq(-3, 3, by = 0.5)) + # Define x limits, add ticks.
      scale_y_continuous(limits = c(0, 6), breaks = seq(0, 6, by = 1)) + # Define y limits, add ticks.
    
      theme(
        plot.title = element_text(family = "Arial", size = 11, hjust = 0), # Title size and font.
        plot.subtitle = element_text(family = "Arial", size = 11), # Subtitle size and font.
        axis.text = element_text(family = "Arial", size = 10), # Size and font of x and y values.
        axis.title = element_text(family = "Arial", size = 10), # Size and font of x and y axes.
        panel.border = element_rect(colour = "black", fill = NA, size = 1), # Black border around the plot area.
        axis.ticks = element_line(colour = "black", size = 1), # Style of x and y ticks.
        legend.position = "none"
      ) + # Remove legend.
    
      geom_hline(yintercept = 1.30103, colour = "black", linetype = "dashed", size = 0.75) + # Horizontal significance cut-off line.
      geom_vline(xintercept = 0.584963, colour = "black", linetype = "dashed", size = 0.75) + # Vertical significance cut-off line (+).
      # geom_vline (xintercept = -0.584963, colour = "black", linetype = "dashed", size = 0.75) #Vertical significance cut-off line (-)
    
      scale_fill_manual(breaks = c("blue", "red"), values = c("deepskyblue3", "firebrick1")) + # Costum colors of data points based on "PursFur" column.
    
      geom_text(aes(label = ifelse(PursFur == 1, as.character(Protein.ID), "")), hjust = 0, vjust = -0.25) + # Add identifiers to a subset of data points.
    
      annotate("text", x = 2.9, y = 1.45, label = "P = 0.05", size = 4, fontface = "bold") + # Label to horizontal cut-off line.
      annotate("text", x = 0.68, y = 5.9, label = "1.5-fold", size = 4, fontface = "bold", srt = 90) + # Label to vertical cut-off line.
      annotate("rect", xmin = 0.584963, xmax = 3, ymin = 1.30103, ymax = 6, alpha = .2) # Shade plot subregion.
    

    最佳答案

    正如@hrbrmstr和@ zx8754的注释所建议的,这是我对上面的代码所做的修改。

    解决阴影问题(通过@hrbrmstr):

    annotate ("rect", xmin = 0.584963, xmax = Inf, ymin = 1.30103, ymax = Inf, alpha = .2)
    

    要解决标签问题(通过@ zx8754):
    geom_label_repel (aes (label = ifelse (PursFur == 1, as.character (Protein.ID), '')), nudge_x = 1.3, direction = "x")
    

    这是这两个更改之后的结果:

    enter image description here

    请参阅this websitethis nice ggrepel tutorial,进一步探讨我的初始问题的第二部分。

    关于r - 如何遮盖绘图子区域并使用ggrepel标记数据点的子集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53272541/

    相关文章:

    r - 如何使用数据框中的当前行值检查前一行值

    R中数据帧中的舍入值

    r - 将平均值添加到构面

    r - 各个条上的数字对齐

    graph - 如何利用无向图中的已知信息进行预测

    r - ggplot2 - 添加 alpha 参数使 PDF 中的所有其他文本变暗/加粗

    r - 基于每组行数的子集数据帧

    r - 如何使scale_x_date周从星期日开始

    c++ - 在 C/C++ 中验证 DNA

    r - 优化网络邻接矩阵创建功能