ggplot图例不适用于scale_colour_manual [英] ggplot legend not working with scale_colour_manual
本文介绍了ggplot图例不适用于scale_colour_manual的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I know an identical question has been asked earlier. ggplot legend - scale_colour_manual not working
But the question involves a somewhat complicated dataset than what I have here and the answer suggests restructuring data and then works with restructured data. But the problem persists even with simple data as I have below and I can't solve it. So please don't mark it as duplicate.
The problem: when using scale_colour_manual
in ggplot2, the legend is not showing.
p <- data.frame(a = runif(10, 1, 2))
ggplot(data=p, aes(x=a)) +
geom_histogram() +
geom_vline(aes(xintercept=mean(p$a), colour="mea")) +
geom_vline(aes(xintercept=median(p$a), colour="med")) +
scale_colour_manual(name="Statistic",
values=c("med"= "red", "mea"="green"))
Any help is appreciated.
解决方案
You have to use show_guide=TRUE
in geom_vline
(defaults to FALSE
):
p <- data.frame(a = runif(10, 1, 2))
ggplot(data=p, aes(x=a)) +
geom_histogram() +
geom_vline(aes(xintercept=mean(a), colour="mea"), show_guide=TRUE) +
geom_vline(aes(xintercept=median(a), colour="med"), show_guide=TRUE) +
scale_colour_manual(name="Statistic",
values=c("med"= "red", "mea"="green"))
这篇关于ggplot图例不适用于scale_colour_manual的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文