1

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!

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • Please make your [example reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by providing sample data and expected output. I don't understand what you mean by *"one has no adjust value and the other one has"*. – Maurits Evers Jun 07 '18 at 23:14
  • Within this plot, I want to compare two density curves - one where I want to add an adjust value and one with the standard bandwidth where adjust=1. Both density curves are meant to represent the same data. I now added a link to the .txt file that I used. – Dominik Rolph Jun 07 '18 at 23:18

1 Answers1

1

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

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • Hi, then I come up with another question; ggplot doesn't show expected results. My goal was to determine a density curve for each Stimulus where the density max meet a specific number. This is done using a for loop. When I do `df<-subset(df, Stimulus=="Problema1.rtf"`for example and then do `plot(density(as.numeric(df$value, bw="SJ", adjust=4.3))` a different curve comes out compared to the `ggplot` command using an adjust of 4.3 for the blue curve, for a comparison see: https://drive.google.com/drive/folders/1bVcm4B75YAApFB-1y4N-kYfWXzzhIl7o?usp=sharing – Dominik Rolph Jun 08 '18 at 10:11
  • Hi, here: https://stackoverflow.com/questions/18162425/the-difference-between-geom-density-in-ggplot2-and-density-in-base-r I found the information I now wanted - they geom_density and density() apparently use different values which was the reason why I was wondering about the results. When I do it like Chase in this post proposed, I get the results I want. Thanks a lot! – Dominik Rolph Jun 08 '18 at 10:20
  • @DominikGrätz You're very welcome; glad it worked out! – Maurits Evers Jun 08 '18 at 15:40