1. 程式人生 > >R繪圖(3): 散點圖新增文字註釋

R繪圖(3): 散點圖新增文字註釋

這裡以火山圖為例進行說明,在轉錄組分析中,火山圖是很常見的一類圖,縱軸表示p_value,橫軸表示log (fold change)。單一的散點圖繪製很簡單,火山圖比較難處理的地方就是一些基因的註釋,基因越多,加文字註釋越困難,因為文字會堆在一起,看不清。 示例資料df1是轉錄組做差異表達後的部分結果,df2將logFC絕對值大於0.3的挑出來了。 ``` > head(df1) p_val avg_logFC class gene cd avg_logFC_new2 p_val_new 1 1.628043e-43 0.4804759 P2L PI3 P2L_0.3 -0.4804759 42.78833 2 1.131599e-88 0.4565683 P2L ZG16 P2L_0.3 -0.4565683 87.94631 3 7.342746e-58 0.4192149 P2L XIST P2L_0.3 -0.4192149 57.13414 4 1.728085e-28 0.4113532 P2L FN1 P2L_0.3 -0.4113532 27.76243 5 1.288611e-33 0.4100842 P2L PIGR P2L_0.3 -0.4100842 32.88988 6 6.647279e-14 0.4035325 P2L HSPA1A P2L_0.3 -0.4035325 13.17736 df2=df1%>%filter(abs(avg_logFC_new2) > 0.3) ``` 先看一下,沒加文字的圖 ``` p <- ggplot(data = df1,aes(x = avg_logFC_new2, y = p_val_new)) + geom_point(aes(colour = cd,size = abs(avg_logFC_new2)),alpha=0.9) + scale_color_manual(values=c("P2L_0.3" = "#80B1D3","else" = "grey","L2P_0.3" = "#FB8072"))+ scale_x_continuous("avg_logFC",limits = c(-0.6,0.6),breaks = seq(-0.6,0.6,0.3),labels = seq(-0.6,0.6,0.3)) + scale_y_continuous("-log10 (p-value)")+ geom_vline(xintercept=c(-0.3,0.3),lty=2,col="black",lwd=1) + theme_bw()+ theme( legend.background=element_blank(), legend.key=element_blank(), legend.title = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank() ) p ggsave("tmp0.pdf",width = 22, height = 20, units = c("cm")) ``` ![file](https://img2020.cnblogs.com/other/2265439/202103/2265439-20210308164842227-783052932.png) 接下來用ggplot2裡面的geom_text新增文字,另建一個圖層,在新圖層中指定data和mapping,需要注意的是,新圖層裡面沒有指定x和y,則會延用之前圖層的x和y,也就是前面的`x = avg_logFC_new2, y = p_val_new` ``` p+geom_text(data=df2,mapping = aes(label=gene)) ggsave("tmp1.pdf",width = 22, height = 20, units = c("cm")) ``` ![file](https://img2020.cnblogs.com/other/2265439/202103/2265439-20210308164842445-125580347.png) 這張圖存在兩個問題:文字直接蓋在點上,遮住了點;文字相互重疊。 再看一下ggplot2的另一個函式geom_label ``` p+geom_label(aes(label=gene),df2,alpha=0,nudge_y = 3) #alpha=0讓文字框的背景透明,讓點顯露出來;nudge_y把註釋框上移 ggsave("tmp2.pdf",width = 22, height = 20, units = c("cm")) ``` ![file](https://img2020.cnblogs.com/other/2265439/202103/2265439-20210308164843049-1060504443.png) 可以看到,文字框還是重疊... 這裡介紹一下我用的另一個R包`ggrepel`,它就是解決這個問題的 ``` p+ggrepel::geom_text_repel( aes(label=gene),df2 ) ggsave("tmp3.pdf",width = 22, height = 20, units = c("cm")) ``` ![file](https://img2020.cnblogs.com/other/2265439/202103/2265439-20210308164843316-817243999.png) 這個圖裡面重疊問題已經解決了,文字靠在點的旁邊,且文字不重疊,太密集的區域有線段指向。不過大部分沒有線段指向,如果點與點,文字與文字比較近,還是無法肉眼區分,最好再多加一些線段來指向。可以調整一下幾個padding引數,如下: ``` p+ggrepel::geom_text_repel( aes(label=gene,color=cd),df2, size = 4, #註釋文字的字型大小 box.padding = 0.5, #字到點的距離 point.padding = 0.8, #字到點的距離,點周圍的空白寬度 min.segment.length = 0.5, #短線段可以省略 segment.color = "black", #segment.colour = NA, 不顯示線段 show.legend = F) ggsave("tmp4.pdf",width = 22, height = 20, units = c("cm")) ``` ![file](https://img2020.cnblogs.com/other/2265439/202103/2265439-20210308164843568-115270681.png) 到這兒,文字註釋算加完了,線段的方向還不是很滿意,有些雜亂文章。 這個包裡面的另一個函式geom_label_repel,可以加文字框,引數和geom_text_repel類似, ``` p+ggrepel::geom_label_repel( aes(label=gene),df2 ) ggsave("tmp5.pdf",width = 22, height = 20, units = c("cm")) ``` ![file](https://img2020.cnblogs.com/other/2265439/202103/2265439-20210308164843852-1064189695.png) 需要注意的是,文字框會遮住點,調節alpha引數的話,會同時改變文字框背景和文字的透明度,這個和ggplot2裡面geom_label的alpha引數不太一樣。 > 因水平有限,有錯誤的地方,歡迎批評