【问题标题】:ggplot: How to keep marker colours in legend but hide text colours?ggplot:如何在图例中保留标记颜色但隐藏文本颜色?
【发布时间】:2018-12-13 12:34:57
【问题描述】:

我希望更改一些(但不是全部)标签的颜色,以便它们在填充图中突出。我使用以下方法实现了这一点:

df <- data.frame("x"=1:10, "y"=1:10, "dir"=rep(c("a", "b"), 5))

library(ggplot2)
library(ggrepel)
ggplot(data=df, aes(x, y, color=dir)) +
  geom_point(show.legend=TRUE) +
  geom_text_repel(data=df[1:5, ], 
                  aes(x=x, y=y, 
                      color=factor(df$dir[1:5], labels=c("text a", "text b")), 
                      label=dir), 
                  size=2.5, force=15, show.legend=FALSE) + 
  scale_colour_manual(values=c("salmon", "black", "salmon", "darkturquoise"))

如何在图例中隐藏标签配色方案的同时执行此操作(即在下图中去掉文本 a 和文本 b)? show.legend=FALSE 似乎不起作用,我想保留点图例。

在这个例子中,我使用的是geom_text_repel,但我想geom_text 也是一样的。

谢谢!

【问题讨论】:

    标签: r ggplot2 ggrepel


    【解决方案1】:

    您也可以通过将最后一行替换为

    来手动指定中断
    scale_colour_manual(breaks = c("a", "b"), values = c("salmon", "black", "salmon", "darkturquoise"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      相关资源
      最近更新 更多