我想将密度曲线叠加到我构建的频率直方图上。对于频率直方图,我使用了 aes(y=..counts../40)
,因为 40 是我的总样本数。我使用 aes(y=..density..*0.1)
强制密度介于 0 和 1 之间,因为我的 binwidth 是 0.1。但是,密度曲线不适合我的数据,它排除了等于 1.0 的值(请注意,直方图显示 bin=(1.0,1.1) 的累积值,但密度曲线以 1.0 结束)
这是我的数据
data<-structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("E1", "test"
), class = "factor"), value = c(0.288888888888889, 0.0817901234567901,
0.219026548672566, 0.584795321637427, 0.927554980595084, 0.44661095636026,
1, 0.653780942692438, 1, 0.806451612903226, 1, 0.276794335371741,
1, 0.930109557990178, 0.776864728192162, 0.824909747292419, 1,
1, 1, 1, 1, 0.0875912408759124, 0.308065494238933, 1, 0.0258064516129032,
0.0167322834645669, 1, 1, 0.355605889014723, 0.310344827586207,
0.106598984771574, 0.364447494852436, 0.174724342663274, 0.77491961414791,
1, 0.856026785714286, 0.680759275237274, 0.850657108721625, 1,
1, 0, 0.851851851851852, 1, 0, 0.294954721862872, 0.819870009285051,
0, 0.734147168531706, 0.0135424091233072, 0.0189098998887653,
0.0101010101010101, 0, 0.296905222437137, 0.706837929731772,
0.269279393173198, 0.135379061371841, 0.158969804618117, 0.0902981940361193,
0.00423131170662906, 0, 0.374880611270296, 0.0425790754257908,
0.145542753183748, 0, 0.129032258064516, 0.260334645669291, 0,
0, 1, 0.175505350772889, 0.08248730964467, 0, 0.317217981340119,
0.614147909967846, 0, 0.264508928571429, 0.883520276100086, 0.0657108721624851,
0, 0.560229445506692)), row.names = c(NA, -80L), .Names = c("variable",
"value"), class = "data.frame")
阴谋
q<-ggplot(data, aes(value, fill = variable))
q + geom_density(alpha = 0.6,aes(y=..density..*0.1),binwidth=0.1)
+ theme_minimal()+scale_fill_manual(values =c("#D7191C","#2B83BA"))
+ theme(legend.position="bottom")+ guides(fill=guide_legend(nrow=1))
+ labs(title="Density Plot GrupoB",x="Respuesta",y="Density")
+scale_x_continuous(breaks=seq(from=0,to=1.2,by=0.1))
+geom_histogram(alpha = 0.6,aes(y=..count../40),binwidth=0.1,position="dodge")
我得到的输出是这个
最佳答案
您的情节完全符合您的数据的预期:
data$value
,其中包含介于 0 和 1 之间的数值,因此您应该期望密度曲线也从 0 运行到 1。 我认为以您的方式绘制直方图是一个坏主意。原因是对于直方图,x 轴是连续的,这意味着覆盖 x 轴范围从 0.1 到 0.2 的条表示(包括)0.1 和 0.2 之间的值的计数(不包括后者)。在这种情况下使用闪避会导致图片失真,因为条形现在不再覆盖正确的 x 轴范围。两个条共享它们应该完全覆盖的范围。这种失真是密度曲线似乎与直方图不匹配的原因之一。
所以你能对它做点啥?我可以给你一些建议,但也许其他人有更好的想法......
position="dodge"
将直方图彼此相邻绘制,您可以使用分面,即将直方图(和相应的密度曲线)绘制成单独的图。这可以通过添加 + facet_grid(variable~.)
来实现到你的情节。 data$value[data$value==1]<-0.999
.重要的是您只为绘图执行此操作,这实际上仅意味着您稍微重新定义了分箱。对于您缩进要做的所有数字评估,您不应该进行此替换! (例如,它会改变 data$value
的平均值。)geom_density(alpha = 0.6,aes(y=..density..))
(我还删除了 bindwith=0.1
因为它对 geom_density
没有影响)和 geom_histogram(alpha = 0.6,aes(y=..count../40/.1),binwidth=0.1)
(不需要 position="dodge"
,一旦你使用刻面)。当然,这会导致您所拥有的相对归一化,但它更有意义,因为密度曲线和直方图上的积分应该是 1。 adjust
来更好地理解它是如何工作的。至geom_density
.对于较小的数字,它会使曲线不太平滑,并且曲线将更接近直方图。 为了将所有内容放在一起,我已将所有建议构建到您的代码中,使用
adjust=0.2
在 geom_density
并绘制结果:data$value[data$value==1]<-0.999
q<-ggplot(data, aes(value, fill = variable))
q + geom_density(alpha = 0.6,aes(y=..density..),adjust=0.2) +
theme_minimal()+scale_fill_manual(values =c("#D7191C","#2B83BA")) +
theme(legend.position="bottom")+ guides(fill=guide_legend(nrow=1)) +
labs(title="Density Plot GrupoB",x="Respuesta",y="Density")+
scale_x_continuous(breaks=seq(from=0,to=1.2,by=0.1))+
geom_histogram(alpha = 0.6,aes(y=..count../40/.1),binwidth=0.1) +
facet_grid(variable~.)
不幸的是,我不能给你一个更完整的答案,但我希望这些想法能给你一个好的开始。
关于r - 叠加密度图不包括直方图值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30288577/