user434180
user434180

Reputation: 805

Adding a legend to these two histograms in R

I find R and ggplot to be extremely unintuitive. So any guidance would be appreciated. I have already tried putting the two vectors I want to make histograms from into a data frame, but I could never get the alpha value of each histogram to be different.

So now I use the code:

ggplot(mydata) + geom_histogram(aes(x=mydata$D2prediction75At,y = ..density..,color = "blue"),color="darkblue", fill="blue",bins=41, alpha=0.75) 
 + geom_histogram(aes(x=mydata$D2y,y = ..density..,color = "red"),color="darkblue", fill="red",bins=41, alpha=0.5) 

and some other options, hopefully not relevant:

+ theme_pander() + ggtitle("Histograms") +xlab("Subjective Elicitations")+ylab("Density")+theme(  panel.border = element_blank(),  panel.grid.major = element_blank(),  panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) + theme(plot.title = element_text(hjust = 0.5))  

to produce:

enter image description here

All I want is to add a legend showing the red histogram represents data A and the blue represents data B.

This seems to be a very difficult task in R.

Upvotes: 1

Views: 1300

Answers (3)

Duck
Duck

Reputation: 39595

You can also try next approach, in addition of the good advice of @GregorThomas. First an example using iris dataset, you need to pass the fill option inside aes() and then use scale_fill_manual():

library(ggplot2)
#Example
ggplot(iris)+
  geom_histogram(aes(x=Sepal.Length,y = ..density..,color = "blue",fill='B'),
                 color="darkblue",bins=41, alpha=0.75)+
  geom_histogram(aes(x=Sepal.Width,y = ..density..,color = "red",fill='A'),
                 color="darkblue",bins=41, alpha=0.5)+
  scale_fill_manual(values=c("blue","red"),'Var')

Output:

enter image description here

In the case of your code, try this (not tested as no data was shared):

#Code OP
ggplot(mydata) + 
  geom_histogram(aes(x=D2prediction75At,y = ..density..,
                     color = "blue",fill='B'),
                 color="darkblue",bins=41, alpha=0.75) +
  geom_histogram(aes(x=D2y,y = ..density..,color = "red",fill='A'),
                 color="darkblue",bins=41, alpha=0.5)+
  scale_fill_manual(values=c("blue","red"),'Data')+
  theme_pander() + 
  ggtitle("Histograms") +
  xlab("Subjective Elicitations")+
  ylab("Density")+
  theme(  panel.border = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          axis.line = element_line(colour = "black")) +
  theme(plot.title = element_text(hjust = 0.5))  

Upvotes: 2

akrun
akrun

Reputation: 887048

We can also do

 mydata_long <- melt(mydata,  measure = c("D2prediction75At", "D2y"))
ggplot(mydata_long, aes(x = value, fill = variable)) +
  geom_histogram(alpha = 0.65, bins = 41, color = "darkblue") +
   scale_fill_manual(values = c(D2prediction75At = "blue", D2y = "red") +
   ... # your other options

Upvotes: 1

Gregor Thomas
Gregor Thomas

Reputation: 145765

ggplot is designed to work with data in long format - in which case legends are produced automatically. I'd suggest converting your data into a format ggplot likes. You haven't shared any data, but based on your ggplot calls, this is my best guess. If you need help debugging, please share a reproducible sample of your data.

mydata_long = pivot_longer(mydata, cols = c(D2prediction75At, D2y))

ggplot(mydata_long, aes(x = value, fill = name)) +
  geom_histogram(alpha = 0.65, bins = 41, color = "darkblue") +
  scale_fill_manual(values = c(D2prediction75At = "blue", D2y = "red") +
  ... # your other options

Upvotes: 2

Related Questions