在很多作圖中豁生,我們都涉及過(guò)添加標(biāo)簽酸纲,例如火山圖捣鲸,一般都是用ggrepel中的geom_text_repel函數(shù)。
構(gòu)建一個(gè)散點(diǎn)圖闽坡。
library(ggplot2)
library(ggrepel)
setwd("C:/Users/tq199/Desktop")
A <- read.csv("A.csv", header = T)
ggplot(A, aes(x=rank, y=avg_log2FC)) +
geom_point(size=3, color='#DC050C')+
theme_bw()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
axis.title = element_text(colour = 'black', size = 15),
axis.text = element_text(colour = 'black', size = 12))+
labs(x='Rank', y='Log2FC')
需要標(biāo)簽的點(diǎn)整理一個(gè)單獨(dú)的文件栽惶,直接使用geom_text_repel,設(shè)置下箭頭疾嗅、大小外厂、字體等等。但是我們會(huì)發(fā)現(xiàn)一個(gè)問(wèn)題代承,那就是有些標(biāo)簽不會(huì)顯示(顯示不全)酣衷,這是因?yàn)樘鄬?dǎo)致重復(fù)了。
B <- read.csv("B.csv", header = T)
ggplot(A, aes(x=rank, y=avg_log2FC)) +
geom_point(size=3, color='#DC050C')+
geom_point(data = A[abs(A$avg_log2FC)<0.5,],
stroke = 0.5, size=3, shape=16, color="grey50",alpha =0.4)+
theme_bw()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
axis.title = element_text(colour = 'black', size = 15),
axis.text = element_text(colour = 'black', size = 12))+
labs(x='Rank', y='Log2FC')+
geom_text_repel(data=B, aes(label=X), color="black", size=4, fontface="italic",
arrow = arrow(ends="first", length = unit(0.01, "npc")), box.padding = 0.2,
point.padding = 0.3, segment.color = 'black', segment.size = 0.3, force = 1, max.iter = 3e3)
在很多文章中次泽,我們發(fā)現(xiàn)別人的標(biāo)簽排列很整齊,這里只需要設(shè)置兩個(gè)小參數(shù)即可席爽。第一direction設(shè)置y軸對(duì)齊意荤,nudge_x設(shè)置指示線長(zhǎng)一點(diǎn)即可。
B1 <- B[B$avg_log2FC>0,]
B2 <- B[B$avg_log2FC<0,]
ggplot(A, aes(x=rank, y=avg_log2FC)) +
geom_point(size=3, color='#DC050C')+
geom_point(data = A[abs(A$avg_log2FC)<0.5,],
stroke = 0.5, size=3, shape=16, color="grey50",alpha =0.4)+
theme_bw()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
axis.title = element_text(colour = 'black', size = 15),
axis.text = element_text(colour = 'black', size = 12))+
labs(x='Rank', y='Log2FC')+
geom_text_repel(data=B1, aes(label=X), color="black", size=4, fontface="italic",
size=3, segment.size=0.5, nudge_x=500, direction="y", hjust=0)+
geom_text_repel(data=B2, aes(label=X), color="black", size=4, fontface="italic",
size=3, segment.size=0.5, nudge_x=-1000, direction="y", hjust=0)
最后只锻,如果需要標(biāo)注的點(diǎn)比較散玖像,也可以使用x對(duì)齊。
B3 <- B2[abs(B2$avg_log2FC)>1.2,]
ggplot(A, aes(x=rank, y=avg_log2FC)) +
geom_point(size=3, color='#DC050C')+
geom_point(data = A[abs(A$avg_log2FC)<0.5,],
stroke = 0.5, size=3, shape=16, color="grey50",alpha =0.4)+
theme_bw()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
axis.title = element_text(colour = 'black', size = 15),
axis.text = element_text(colour = 'black', size = 12))+
labs(x='Rank', y='Log2FC')+
geom_text_repel(data=B3, aes(label=X), color="black", size=4, fontface="italic",
size=3, segment.size=0.5, nudge_x=-1000, direction="x", hjust=0)
參數(shù)
大部分geom_text()的參數(shù)都適用于geom_text_repel(),除了以下幾個(gè):
hjust
vjust
position
check_overlap
ggrepel包為geom_text_repel()與geom_label_repel()提供了特有的參數(shù)設(shè)置:
segment.color:連接點(diǎn)與標(biāo)簽的線段的顏色
segment.size:線段的粗細(xì)
segment.alpha:線段的透明度
box.padding:文本框周邊填充
point.padding:點(diǎn)周?chē)畛?arrow:grid:arrow提供的箭頭
force:強(qiáng)制性將重疊文本散開(kāi)
max.oter:最大迭代次數(shù)
nudge_x/y:標(biāo)簽開(kāi)始位置在坐標(biāo)軸的移動(dòng)距離
direction:允許標(biāo)簽的方向齐饮,x捐寥、y or both
下面舉個(gè)栗子來(lái)詳細(xì)了解這些參數(shù)的圖形效果
set.seed(42)
ggplot(mtcars)+ geom_point(aes(wt, mpg, color=factor(cyl)), size=3)+
geom_text_repel(aes(wt, mpg, color=factor(cyl),
label=rownames(mtcars), angle=ifelse(mtcars$cyl==4, 90, 0)),
size=4, family="Times", fontface="bold",
box.padding=unit(0.5, "lines"), point.padding=unit(1.6, "lines"),
segment.color = "#cccccc", segment.size = 0.5,
arrow = arrow(length=unit(0.01, "npc")),force = 1, max.iter = 3e3,
nudge_x = ifelse(mtcars$cyl==6, 2, 0), nudge_y = ifelse(mtcars$cyl==6, 9, 0))+
scale_color_discrete(name="cyl")+
scale_x_continuous(expand = c(0.5, 0))+
scale_y_continuous(expand = c(0.25, 0))+
theme_classic(base_size = 16)
參考
ggplot文字標(biāo)簽設(shè)置(geom_text_repel函數(shù))的一個(gè)小例子
R語(yǔ)言可視化學(xué)習(xí)筆記之ggrepel包