使用gghighlight软件包标记点时,如何保留图表图例

时间:2019-03-19 14:35:07

标签: r ggplot2

我有一个看起来像这样的数据框:

  rowname  Class Sec    ES.2um Mean_WPBs   ES.2um_ZS   Mean_ES  VWF_Sec    name
        1 Formin HAI 113.37340  147.1792  0.16078492 131.69309 112.5219  DIAPH1
        2 Formin PMA  43.90661  121.9017 -0.11594028  75.37296 137.4212    FMN2
        3 Septin HAI  64.32138  132.7591 -0.16218581  66.23765 150.9011 SEPTIN5
        4 Septin PMA  53.15791  145.7871 -0.86969449  81.92690 140.2647   LRCH3
        5 Arp2/3 PMA  68.67222  161.0516 -0.05404113  82.51804 158.2623   ARPC3
        6 Arp2/3 HAI  71.00643  149.0704 -0.38119473  82.91458 130.5494   WASF3

在“秒”列中,该行可以是“ HAI”或“ PMA”。我目前正在使用gghighlight来识别一类蛋白质:

plot_ll <- ggplot(df, aes(ES.2um_ZS, VWF_Sec, col = Sec,)) + 
    geom_point(size = 2.5) + 
    geom_point(aes(col=Sec)) + 
    geom_point() + 
    labs (col="Secretaogue") + 
    xlim(-1.25,0.7) + ylim(70,178) + 
    scale_colour_manual(values=c("HAI" = "blue", "PMA" = "red")) + 
    gghighlight(Class == "Formin", use_direct_label = FALSE, label_key = name, unhighlighted_colour = alpha("green", 0.0)) + 
    ggtitle("Formin Proteins Highlighted") + 
    theme_bw() + 
    theme(plot.title = element_text(hjust =0.5)) + 
    xlab("Mean Exit Site Z-Score (Area >2um)") + ylab("Secretion") + 
    geom_hline(yintercept = mean_VWF, color = "black", linetype = "dashed") + 
    annotate("text", -1.2, 173, label="Mean", color = "black") + 
    geom_vline (xintercept = mean_ES.Z, color = "black", linetype = "dashed") + 
    annotate("text", 0.5, 70, label = "Mean", color = "black")

哪个给出的内容看起来像这样(抱歉,这与上面的示例数据框不太一样,但是出于问题的目的,该数据将给出类似的图表):

Scatter plot without labels

请注意,gghighlight正在工作,只是漂白点的alpha值设置为0,所以您看不到它们。

我还想用“名称”列中的蛋白质名称标记这些点,因此启用use_direct_label:

plot_ll <- ggplot(df, aes(ES.2um_ZS, VWF_Sec, col = Sec,)) + 
    geom_point(size = 2.5) +
    geom_point(aes(col=Sec)) +
    geom_point() +
    labs (col="Secretaogue") +
    xlim(-1.25,0.7) + ylim(70,178) +
    scale_colour_manual(values=c("HAI" = "blue", "PMA" = "red")) +
    gghighlight(Class == "Formin", use_direct_label = TRUE, label_key = name, unhighlighted_colour = alpha("green", 0.0)) +
    ggtitle("Formin Proteins Highlighted") +
    theme_bw() +
    theme(plot.title = element_text(hjust =0.5)) +
    xlab("Mean Exit Site Z-Score (Area >2um)") +
    ylab("Secretion") + 
    geom_hline(yintercept = mean_VWF, color = "black", linetype = "dashed") + 
    annotate("text", -1.2, 173, label="Mean", color = "black") + 
    geom_vline (xintercept = mean_ES.Z, color = "black", linetype = "dashed") + 
    annotate("text", 0.5, 70, label = "Mean", color = "black")

这当然摆脱了传说。但是,现在无法知道哪些是“ HAI”点,哪些是“ PMA”:

Scatter plot with labels

我如何保留图例以区分HAI和PMA,同时用相应的蛋白质名称标记每个点?

一种方法可能只是手动添加图例-这可能吗?

任何帮助将不胜感激

0 个答案:

没有答案