[英]Add a legend for a geom_text layer to explain labels
考慮以下示例,其中進行了散射,並且僅對“重要”點進行了着色和標記。
genes <- read.table("https://gist.githubusercontent.com/stephenturner/806e31fce55a8b7175af/raw/1a507c4c3f9f1baaa3a69187223ff3d3050628d4/results.txt", header = TRUE)
genes$Significant <- ifelse(genes$padj < 0.05, "FDR < 0.05", "Not Sig")
ggplot(genes, aes(x = log2FoldChange, y = -log10(pvalue))) +
geom_point(aes(color = Significant)) +
scale_color_manual(values = c("red", "grey")) +
theme_bw(base_size = 12) + theme(legend.position = "bottom") +
geom_text_repel(
data = subset(genes, padj < 0.05),
aes(label = Gene),
size = 5,
box.padding = unit(0.35, "lines"),
point.padding = unit(0.3, "lines")
)
現在想象標簽實際上是首字母縮略詞,並且它們有一個真正的全長名稱(例如,“DOK6”是“Duo Ocarino Kayne 6”的首字母縮寫詞)。 是否可以在 plot 中添加圖例,其中鍵是 plot 上使用的標簽,條目是標簽的全長名稱?
首先,我為另一個僅顯示重要Gene
的圖例添加了Gene2
。\
接下來, Gene2
作為fill
添加到aes
上。 (只有color
會影響geom_point
上點的顏色)。\
最后,為第二個圖例添加了scale_fill_discrete
。 您需要做的只是在Full names here
處注釋全長名稱列。
genes$Gene2 <-ifelse(genes$padj<0.05, genes$Gene, NA)
ggplot(genes, aes(x = log2FoldChange, y = -log10(pvalue),
fill=Gene2)) +
geom_point(aes(color = Significant)) +
scale_color_manual(values = c("red", "grey")) +
theme_bw(base_size = 12) + theme(legend.position = "bottom") +
geom_text_repel(
data = subset(genes, padj < 0.05),
aes(label = Gene),
size = 5,
box.padding = unit(0.35, "lines"),
point.padding = unit(0.3, "lines")
) +
scale_fill_discrete(labels=paste0(genes$Gene,': ',' Full names here'),
name='Significant genes') +
theme(legend.position = 'right')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.