0

I'm struggling with how to add a legend to my plot. I'm plotting on the same figure 4 density plots coming from 4 differents dataframes

  • each one of them is made of a single column
  • The column name has the same name as the R object (colnames(df1)='df1')
  • The number of lines varies, but rownames can be not unique from a df to another

The code is :

ggplot() +
    geom_density(data=df1, aes(x=df1), color='black', fill='black', alpha = 0.2) +
    geom_density(data=df2, aes(x=df2), color='darkred', fill='darkred',alpha = 0.2) +
    geom_density(data=df3, aes(x=df3), color='darkblue', fill='darkblue',alpha = 0.2) +
    geom_density(data=df4, aes(x=df4), color='darkgreen', fill='darkgreen',alpha = 0.2) +
    xlim(0.5,1) +
    ggtitle('Density plots') +
    xlab('Indices') +
    ylab('Density')

The usual way to add a legend is to merge dataframes, draw each density per group and color by groups ; but in that case, how can I build a legend saying which curve corresponds to which dataframe ?

Thanks.

RLave
  • 8,144
  • 3
  • 21
  • 37
Micawber
  • 707
  • 1
  • 5
  • 19
  • If each of your dataframes is only one column, why not combine them all? – Ben G Feb 28 '19 at 14:08
  • because I'm struggling even more with R datatypes ^^. What is the most efficient way to do that ? – Micawber Feb 28 '19 at 14:11
  • 2
    You answered your question yourself - the most efficient way is to "merge dataframes, draw each density per group and color by groups" – pogibas Feb 28 '19 at 14:19
  • @PoGibas I did but R made bullshit with the type of my values ; I had to export the data as a csv file and read it to plot it correctly ... I wanted a cleaner way to code. – Micawber Feb 28 '19 at 14:21
  • 2
    Based on your last comment, it doesn't seem to be a `ggplot` or legend issue then. – Ben G Feb 28 '19 at 14:24
  • 2
    Please read and edit your question according to [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so that other users can help you. – pogibas Feb 28 '19 at 14:25

1 Answers1

0

One way to get the legend would be to add the color and fill inside the aesthetics mapping and give them the name you want to show in the legend, and then give both scales the same name and values with scale_...._manual)(), like this:

library(ggplot2)
# dummy data
df1 <- data_frame(df1 = runif(100))
df2 <- data_frame(df2 = runif(100))
df3 <- data_frame(df3 = runif(100))
df4 <- data_frame(df4 = runif(100))

ggplot() +
  geom_density(data=df1, aes(x=df1, color='df1', fill='df1'), alpha = 0.2) +
  geom_density(data=df2, aes(x=df2, color='df2', fill='df2'), alpha = 0.2) +
  geom_density(data=df3, aes(x=df3, color='df3', fill='df3'), alpha = 0.2) +
  geom_density(data=df4, aes(x=df4, color='df4', fill='df4'), alpha = 0.2) +
  scale_color_manual(name = 'Title', values=c('black', 'darkred', 'darkblue', 'darkgreen')) + 
  scale_fill_manual(name = 'Title', values=c('black', 'darkred', 'darkblue', 'darkgreen')) +
  xlim(0.5,1) +
  ggtitle('Density plots') +
  xlab('Indices') +
  ylab('Density')
DS_UNI
  • 2,600
  • 2
  • 11
  • 22
  • 2
    This may answer the question, but what if you had 10 `data.frame`? or 100? This is not exactly how `ggplot` works. Based on the comments I'd wait for OP to improve the question with a more complete example. – RLave Feb 28 '19 at 14:53
  • 1
    I voted this as a useful comment, because I agree, ggplot2 was not intended to work this way, however my answer was intended to be somewhat general as to show how one can work with legends and scales in ggplot2, and all within the knowledge of "asker" (i.e. without changing too much in the provided code) and for that I don't need anymore info – DS_UNI Feb 28 '19 at 15:07
  • That's exactly what I was trying to do ; I was close to it, it works thanks to you know, many thanks. – Micawber Feb 28 '19 at 15:11