Search code examples
rggplot2density-plot

Separate stat_density for two density curves in ggplot2


I am trying to produce a ggplot that shows the histogram of the data as well as two density curves in which one has no adjust value and the other one has. I tried the following code:

ggplot(df, aes_string(x=value))+ 
        geom_histogram(aes(y=..density..), colour="grey", fill="grey", alpha=.3)+
        geom_density(colour="red", fill="red", alpha=.3)+
        stat_density(bw="SJ", alpha=0)+
        geom_density(colour="blue", fill="blue", alpha=.3)+
        stat_density(bw="SJ", adjust=5, alpha=0)+
        theme_bw()

But this produces this graph with both curves overlapping 100%...

enter image description here

The .txt dataframe used is on my google drive Thanks in advance!


Solution

  • Does adding a specific adjust argument to geom_density not do what you want?

    ggplot(df, aes(x=value))+ 
            geom_histogram(aes(y=..density..), colour="grey", fill="grey", alpha=.3)+
            geom_density(colour="red", fill="red", alpha=.3, adjust = 1)+
            geom_density(colour="blue", fill="blue", alpha=.3, adjust = 2)+
            theme_bw()
    

    enter image description here