仅显示 ggplot 图例的特定标签
Only display specific labels of a ggplot legend
我有一些数据点,我想在我的可视化中挑出一些点。我会这样做:
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()
我的问题是黑点很容易解释,不需要标签。我希望只是 显示红色"special" 标签。有什么方法可以隐藏 "normal" 标签吗?
如果您愿意接受红色以外的任何颜色:
ggplot(df) +
geom_point(aes(x, y, color = special)) + scale_size(guide = "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")
我有一些数据点,我想在我的可视化中挑出一些点。我会这样做:
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()
我的问题是黑点很容易解释,不需要标签。我希望只是 显示红色"special" 标签。有什么方法可以隐藏 "normal" 标签吗?
如果您愿意接受红色以外的任何颜色:
ggplot(df) +
geom_point(aes(x, y, color = special)) + scale_size(guide = "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")