-2

I really want to get the legend into the histogram. And I can't figure out what's going on here.

ggplot(data=Male, aes(Male$Naval.Girth)) + 
   geom_histogram(aes(y =..density..), 
             breaks=seq(63, 123, by = 3), 
             col="black", 
             fill="black", 
             alpha=.7) +
   labs(title="Male Naval Girth Measurements", 
             x= "Girth (cm)", y="Density") +
   geom_vline(aes(xintercept=mean(Male$Naval.Girth, na.rm=T)), show.legend = TRUE, 
             color="red", linetype="dashed", size=1.2) +
   stat_function(fun=dnorm,
           color="red",
           size = 0.7,
           args=list(mean=mean(Male$Naval.Girth), 
           sd=sd(Male$Naval.Girth)))

Thanks.

N Woods
  • 37
  • 1
  • 6
  • 2
    It would be helpful if you could provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including sample data. – Kara Woo Apr 29 '18 at 05:43
  • It's okay, I'm not really familiar with how to supply information on this site. I'm still learning R, unfortunately, I don't have time to figure out how to achieve what you suggest (as easy as it may be), thanks anyway. It's for an assignment due tonight and the important part is how we interpret the data, so I'll just leave it. – N Woods Apr 29 '18 at 06:19
  • @NWoods did you get to trying the solution I posted? Do you need further help? – onlyphantom Apr 29 '18 at 06:52

1 Answers1

-1

ggplot by default assumes show.legend=NA in most of the plot types anyway unless specified otherwise through the theme() function or through show.legend=FALSE.

When show.legend=NA, which is the default, ggplot includes any legend if any aesthetics are mapped.

To get ggplot to automatically add a legend, all you need is to map the col or fill to your aesthetics. That is, you can wrap your fill or col under an aes(). The following code doesn't add a legend:

ggplot(data=Male, aes(Male$Naval.Girth)) + 
   geom_histogram(aes(y =..density..), 
             breaks=seq(63, 123, by = 3), 
             col="black", 
             fill="black", 
             alpha=.7)

But this one does, and notice the aes(fill=Naval.Girth) aesthetic mapping here:

ggplot(data=Male, aes(Male$Naval.Girth)) + 
       geom_histogram(aes(y =..density.., fill=Naval.Girth), 
                 breaks=seq(63, 123, by = 3), 
                 col="black", 
                 alpha=.7)

So again, if you want a legend, just put the colors of fill parameters under the aes() wrapper.

onlyphantom
  • 8,606
  • 4
  • 44
  • 58
  • I'm not sure if I'm missing any dependent packages for this, but I still can't get it to work. loaded packages are mosaic, readr & summarytools. I realize this is a histogram, so it's not such a big deal. But I'll need to figure it out for different plots. I'll have a play around with it again. Thanks. – N Woods Apr 29 '18 at 07:46
  • There is no package required, as long as `ggplot2` library is loaded because this is essentially a `ggplot` feature. The idea is to move your `fill` and `col` into the `aes()` call like the code I've shown you, and your legend will appear. – onlyphantom Apr 29 '18 at 07:48