ggplot:如何在图例中保留标记颜色但隐藏文本颜色?
ggplot: How to keep marker colours in legend but hide text colours?
我希望更改一些(但不是全部)标签的颜色,以便它们在填充的绘图中脱颖而出。我使用以下方法实现了这一点:
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
也是一样的。
谢谢!
您也可以通过将最后一行替换为
来手动指定中断
scale_colour_manual(breaks = c("a", "b"), values = c("salmon", "black", "salmon", "darkturquoise"))
我希望更改一些(但不是全部)标签的颜色,以便它们在填充的绘图中脱颖而出。我使用以下方法实现了这一点:
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
也是一样的。
谢谢!
您也可以通过将最后一行替换为
来手动指定中断scale_colour_manual(breaks = c("a", "b"), values = c("salmon", "black", "salmon", "darkturquoise"))