【问题标题】:Separate stat_density for two density curves in ggplot2ggplot2中两条密度曲线的单独stat_density
【发布时间】:2018-06-07 23:08:05
【问题描述】:

我正在尝试生成一个显示数据直方图的 ggplot 以及两条密度曲线,其中一条没有调整值,另一条有。 我尝试了以下代码:

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()

但这会产生两条曲线重叠 100% 的图表...

使用的 .txt 数据框是 on my google drive 提前致谢!

【问题讨论】:

  • 请通过提供示例数据和预期输出来创建您的example reproducible。我不明白您所说的“一个没有调整值,另一个有”
  • 在这个图中,我想比较两条密度曲线——一条我想添加调整值,另一条是标准带宽,其中adjust=1。两条密度曲线旨在表示相同的数据。我现在添加了一个指向我使用的 .txt 文件的链接。

标签: r ggplot2 density-plot


【解决方案1】:

geom_density 添加一个特定的adjust 参数不是你想要的吗?

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()

【讨论】:

  • 嗨,那我想出另一个问题; ggplot 未显示预期结果。我的目标是确定密度最大值满足特定数字的每个刺激的密度曲线。这是使用 for 循环完成的。例如,当我执行df<-subset(df, Stimulus=="Problema1.rtf"for 然后执行plot(density(as.numeric(df$value, bw="SJ", adjust=4.3)) 时,与使用蓝色曲线调整4.3 的ggplot 命令相比,会出现不同的曲线,比较参见:drive.google.com/drive/folders/…
  • 嗨,这里:stackoverflow.com/questions/18162425/… 我找到了我现在想要的信息 - 它们 geom_density 和 density() 显然使用不同的值,这就是我想知道结果的原因。当我像这篇文章中提出的 Chase 那样做时,我得到了我想要的结果。非常感谢!
  • @DominikGrätz 不客气;很高兴它成功了!
猜你喜欢
  • 2015-06-16
  • 2015-09-19
  • 2018-06-21
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 2013-12-19
  • 2015-12-19
  • 1970-01-01
相关资源
最近更新 更多