仅显示ggplot图例的特定标签 [英] Only display specific labels of a ggplot legend

查看:142
本文介绍了仅显示ggplot图例的特定标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据点,我想单独列出可视化中的一些要点。我会这样做:

  df = data.frame(
x = 1:4,y = 1: 4,
special = c('normal','normal','normal','special')


ggplot(df)+
geom_point( aes(x,y,color = special))+
scale_color_manual(values = c('red','black'))+
labs(color =)+
theme_bw )



我的问题在于黑点非常自我解释,不需要标签。我只想 出现红色的特殊标签。有没有一种方法可以隐藏正常标签?

解决方案

  ggplot(df)+ 
geom_point(aes(x,y,color = special))+ scale_size =none)+
scale_color_discrete(breaks =special)+ labs(color =)+
theme_bw()

  cols < -  c(normal=black,special=red)
gg < - ggplot(df)+ geom_point(aes(x,y,color = special))+ labs(color =)+ theme_bw()
gg + scale_colour_manual(values = cols,limits =special )


I have some data points, and I want to single out some of the points in my visualization. I would do it like this:

df = data.frame(
  x = 1:4, y = 1:4,
  special = c('normal', 'normal', 'normal', 'special')
)

ggplot(df) +
  geom_point(aes(x, y, color = special)) +
  scale_color_manual(values = c('red', 'black')) +
  labs(color = "") +
  theme_bw()

My issue here is that the black points are very self explanatory and don't need a label. I want just the red "special" label to appear. Is there a way I can hide the "normal" label?

解决方案

If you are open to having any color other than red:

ggplot(df) +
    geom_point(aes(x, y, color = special)) + scale_size(guide = "none") + 
    scale_color_discrete(breaks="special") + labs(color = "") +
    theme_bw()

EDIT :

cols <- c("normal" = "black","special" = "red")
gg <- ggplot(df) + geom_point(aes(x, y, color = special)) + labs(color = "") + theme_bw()
gg + scale_colour_manual(values = cols, limits = "special")

这篇关于仅显示ggplot图例的特定标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆