Home > database >  Adding a legend on multiple kinds of plots
Adding a legend on multiple kinds of plots

Time:10-15

I'm kind of struggling finding a solution about this specific problem and I'm pretty new to this ggplot2 thing.

So here's the deal : I'm trying to put a legend on multiple kinds of graphs in ggplot2 as shown here:

enter image description here

So I tried with this code :

ggplot(data)   
 geom_histogram(aes(x = rain, y = ..density.., fill = group), bins = 15, color = 'lightblue', fill = 'lightsteelblue') 
 geom_density(aes(x = rain, fill = group), color = 'lightsteelblue', fill = 'lightblue', alpha=0.3) 
 scale_fill_manual(name = 'group',labels = c('Density histogram', 'Fitted pdf' ),
                values = c('lightsteelblue','lightblue')) 
 labs(x = "Rain" , y = "f(x)" , title = "Density histogram of the rain")  
 theme(plot.title = element_text(hjust = 0.5))  
 stat_function(fun = dnorm,
          args = list(mean = mean(data$rain),
                      sd = sd(data$rain)),
          col = "red", size = 1) 

but it only gives me the graph without ANY legend shown

enter image description here

I really don't get how this legend thing works with ggplot.

As I said, I'm new to this, I really tried to search for answers and I might be doing things the wrong way or you might be horrified by my way of coding this (sorry about that haha).

Thanks in advance for the help !

CodePudding user response:

Here is a demonstration of some sort. There are two concepts in ggplot2 that are relevant and closely related here, that are:

  • Aesthetics: a value to represent a graphical aspect of a plot
  • Mapping: a link between a data value and an aesthetic

You can give aesthetics directly, by setting an aesthetic in the layer itself. For example, we can make a green histogram:

library(ggplot2)

ggplot(faithful, aes(eruptions))  
  geom_histogram(fill = "limegreen")
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Now, if we're to make this aesthetic a mapping, we have to feed it through the aes() function. Note that the mapping is almost always the first argument of a layer, so we can omit the argument name.

ggplot(faithful, aes(eruptions))  
  geom_histogram(aes(fill = "limegreen"))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

However, as you can see above, this mapping doesn't really makes sense, even though it has a legend. This is because ggplot2 uses scales to represent mappings between data values and aesthetics. In this case, "limegreen" is seen as a data-value and not a graphical value. So when ggplot2 applies a palette to this value through a (default) scale, it comes out different than the graphical interpretation of this value. This link between data-values and graphical elements is displayed in the legend, which is a property of the scale.

Instead, it makes more sense to map a descriptive value to the aesthetic. We can then explicitly state the link between the data-value and aesthetic in the scale_*() function(s).

ggplot(faithful, aes(eruptions))  
  geom_histogram(aes(fill = "Histogram"))  
  scale_fill_manual(values = "limegreen")
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

If you've grasped the idea outlined above, it should then come naturally to extend the legend for other layers.

ggplot(faithful, aes(eruptions))  
  geom_histogram(aes(fill = "Histogram", y = after_stat(density)))  
  geom_density(aes(fill = "Density"), alpha = 0.3)  
  scale_fill_manual(values = c("limegreen", "tomato"))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2022-10-14 by the histogram and pdf of the rain distribution

  • Related