ggplot不会在geom_histogram中显示图例 [英] ggplot does not show legend in geom_histogram

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

问题描述

我有这段代码

  ggplot()
+ geom_histogram(aes(x = V1,y = ..count ..)/ sum(.. count ..)),fill =red,alpha = .4,color =red,data = coding,stat =bin,binwidth = 30)
+ geom_histogram(aes(x = V1,y =(.. count ..)/ sum(.. count ..)),fill =blue,alpha = .4,color =blue,data = lncrna,stat =bin,binwidth = 30)
+ coord_cartesian(xlim = c(0,2000))
+ xlab(Size(nt))
+ ylab(百分比(%))
+ geom_vline(data = cdf,aes(xintercept = rating.mean,color = Labels),linetype =dashed,size = 1)

产生一个美丽的直方图,没有图例:


在我访问同一问题的每篇文章中,他们都会说在 aes 内放置 color 。不过,这并没有给出任何传说。



我试过:

  ggplot()+ geom_histogram(aes(x = V1,y =(.. count ..)/ sum(.. count ..),color =red,fill =red),fill =red ,alpha = .4,color =red,data = coding,stat =bin,binwidth = 30)
+ geom_histogram(aes(x = V1,y =(.. count ..)/ sum (..count ..),color =blue,fill =blue),fill =blue,alpha = .4,color =blue,data = lncrna,stat =bin,binwidth = 30)
+ coord_cartesian(xlim = c(0,2000))
+ xlab(Size(nt))
+ ylab(Percentage(%))
+ geom_vline(data = cdf,aes(xintercept = rating.mean,color = Labels),linetype =dashed,size = 1)

没有成功。



如何在图表中放置图例?

  set.seed(42)
coding < - data.frame(V1 = rnorm(1000))
lncrna < - data.frame(V1 = rlnorm(1000) )


library(ggplot2)
ggplot()+
geom_histogram(aes(x = V1,y =(.. count ..)/ sum(.. count ..),填充=r,颜色=r),alpha = .4,data = coding,stat =bin)+
geom_histogram(aes(x = V1,y =(.. count ..) /sum(..count ..),fill =b,color =b),alpha = .4,data = lncrna,stat =bin)+
scale_colour_manual(name =group ,值= c(r=红色,b=蓝色),标签= c(b=蓝色值,r=红色值))+
scale_fill_manual(name =group,values = c(r=red,b=blue),labels = c(b=蓝色值,r=红色值 ))


I have this code

ggplot()
+ geom_histogram(aes(x=V1, y=(..count..)/sum(..count..)), fill="red", alpha=.4, colour="red", data=coding, stat = "bin", binwidth = 30)
+ geom_histogram(aes(x=V1,y=(..count..)/sum(..count..)), fill="blue", alpha=.4, colour="blue", data=lncrna, stat = "bin", binwidth = 30)
+ coord_cartesian(xlim = c(0, 2000))
+ xlab("Size (nt)")
+ ylab("Percentage (%)")
+ geom_vline(data=cdf, aes(xintercept=rating.mean, colour=Labels), linetype="dashed", size=1)

that produces a beautiful histogram without legend:

In every post I visit with the same problem, they say to put color inside aes. nevertheless, this does not give any legend.

I tried:

ggplot() + geom_histogram(aes(x=V1, y=(..count..)/sum(..count..),color="red", fill="red"), fill="red", alpha=.4, colour="red", data=coding, stat = "bin", binwidth = 30)
+ geom_histogram(aes(x=V1,y=(..count..)/sum(..count..), color="blue", fill="blue"), fill="blue", alpha=.4, colour="blue", data=lncrna, stat = "bin", binwidth = 30)
+ coord_cartesian(xlim = c(0, 2000))
+ xlab("Size (nt)")
+ ylab("Percentage (%)")
+ geom_vline(data=cdf, aes(xintercept=rating.mean, colour=Labels), linetype="dashed", size=1)

without success.

How can I put a legend in my graph?

解决方案

If you don't want to put the data in one data.frame, you can do this:

set.seed(42)
coding <- data.frame(V1=rnorm(1000))
lncrna <- data.frame(V1=rlnorm(1000))


library(ggplot2)
ggplot() + 
  geom_histogram(aes(x=V1, y=(..count..)/sum(..count..), fill="r", colour="r"), alpha=.4, data=coding, stat = "bin") +
  geom_histogram(aes(x=V1,y=(..count..)/sum(..count..), fill="b", colour="b"), alpha=.4, data=lncrna, stat = "bin") +
  scale_colour_manual(name="group", values=c("r" = "red", "b"="blue"), labels=c("b"="blue values", "r"="red values")) +
  scale_fill_manual(name="group", values=c("r" = "red", "b"="blue"), labels=c("b"="blue values", "r"="red values"))

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

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