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.