【问题标题】:Legend disappeared after geom_label_repel() [duplicate]在 geom_label_repel() 之后图例消失了 [重复]
【发布时间】:2021-02-14 10:32:01
【问题描述】:

我有一个简单的data.frame,这是我的脚本,我为每个基因型设置了一种颜色,只是想标记“X”基因型:

ggplot(b, aes(x= V1, y = V2, label = Sample_name)) + 
  geom_point(color = dplyr::case_when(b$Genotype == "X" ~ "#606060", 
                                      b$Genotype == "Y" ~ "#85C1E9",
                                      b$Genotype == "Z" ~ "#2980B9",
             size = 2, alpha = 0.8) +
  geom_label_repel(data = subset(b, Genotype == "X"),
                   box.padding   = 0.35, 
                   point.padding = 0.5,
                   size          = 4,
                   segment.color = 'grey50')

然而,我的传奇消失了,我不明白为什么。我对基因型图例感兴趣。

【问题讨论】:

标签: r ggplot2 legend


【解决方案1】:

您需要在aes 中提供color= 参数,以获得图例,并获得您喜欢分配给事实的颜色,请使用 scale_color_manual 提供:

library(ggplot2)
library(ggrepel)

b = data.frame(V1 = 1:12 , V2 = 0.5*(1:12)^2,
               Sample_name = paste0("s",1:12),
               Genotype = rep(c("X","Y","Z"),each=4))
               
ggplot(b, aes(x= V1, y = V2, label = Sample_name,col=Genotype)) + 
geom_point(size = 2, alpha = 0.8) +
scale_color_manual(values = c("#606060","#85C1E9","#2980B9"))+
geom_label_repel(data = subset(b, Genotype == "X"),
                   box.padding   = 0.35, 
                   point.padding = 0.5,
                   size          = 4,
                   segment.color = 'grey50',
                   show.legend=FALSE)

【讨论】:

    猜你喜欢
    • 2019-11-26
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多