4

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:

enter image description here

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?

user2979409
  • 773
  • 1
  • 12
  • 23
  • 1
    Did you try to specifying fill/color inside the aesthetic mapping? I don't see that in your code. Some [example data](http://stackoverflow.com/a/5963610/1412059) would make it easier to show you how to do that. – Roland Jul 24 '14 at 11:58
  • what is aesthetic mapping? – user2979409 Jul 24 '14 at 12:02
  • @user2979409: It's the `aes()` function you use. – Steve S Jul 24 '14 at 12:03
  • Yes, I tried. I updated my post. – user2979409 Jul 24 '14 at 12:08
  • 2
    If you specify them inside the aes, you should not specify them outside of it. – Roland Jul 24 '14 at 12:14
  • 1
    I would combine `coding` and `lncrna` into one object, with an additional column that would tell the data came from (let's say the variable is called `origin`). You map this in `aes()`, e.g. `ggplot(merged.data, aes(..., fill = origin)) + geom_histogram()`. You can control specific colors through scales. See http://docs.ggplot2.org/current/ – Roman Luštrik Jul 24 '14 at 12:19

2 Answers2

12

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"))

enter image description here

Roland
  • 127,288
  • 10
  • 191
  • 288
5

The problem is that you can't map your color into aes because you've got two separete sets of data. An idea is to bind them, then to apply the "melt" function of package reshape2 so you create a dummy categorical variable that you can pass into aes. the code:

require(reshape2)
df=cbind(blue=mtcars$mpg, red=mtcars$mpg*0.8)
df=melt(df, id.vars=1:2)
ggplot()+geom_histogram(aes(y=(..count..)/sum(..count..),x=value, fill=Var2, color=Var2), alpha=.4, data=df, stat = "bin")

There you've got your legend

agenis
  • 8,069
  • 5
  • 53
  • 102
  • 1
    re: using this, your bars will be either dodged or stacked, but you can tweak the position parameter to superimpose the bars: "position=position_dodge(width=0)" – agenis Jul 24 '14 at 12:38