如何平滑geom_freqpoly以排除零

时间:2017-05-29 23:35:31

标签: r ggplot2 data-visualization

我正试图向我的老板解释小提琴图表,我想使用geom_freqpoly所以我可以说“小提琴图表只是一个侧面翻转的直方图并反映出来”:

library(ggplot2)
df = structure(list(calc = c(0.833333333333333, 1.16666666666667, 1.66666666666667, 1.16666666666667, 1.5, 1.33333333333333, 1.33333333333333, 1.5, 0.833333333333333, 1.83333333333333, 1, 1, 1.5, 1.66666666666667, 1, 1.33333333333333, 0.833333333333333, 0.833333333333333, 1.83333333333333, 1.16666666666667, 1.16666666666667, 1, 0.5, 1.33333333333333, 1, 0.833333333333333, 1.16666666666667, 1.66666666666667, 1.83333333333333, 1.16666666666667, 1.5, 0.833333333333333, 1.5, 1.5, 1.16666666666667, 1.66666666666667, 1, 0.833333333333333, 1.16666666666667, 1, 1, 1.33333333333333)), class = "data.frame", row.names = c(NA, -42L), .Names = "calc")
ggplot(df, aes(x = 1, y = calc)) + geom_violin()
ggplot(df, aes(calc)) + geom_freqpoly(binwidth = 1 / 6) + scale_y_continuous(breaks = 0:10)

问题是小提琴情节如下: enter image description here

,直方图如下所示:enter image description here

换句话说,小提琴曲线会使零点平滑,但 freqpoly 直方图则不会。 如何通过使 geom_freqpoly 平滑零来使两个图表匹配?

1 个答案:

答案 0 :(得分:3)

这不是geom_freqpoly的真实含义,而是表示每个值的实际频率。要进行平滑处理,您需要geom_density

ggplot(df, aes(calc)) + 
    geom_density() + 
    scale_y_continuous(breaks = 0:10)

默认情况下,它看起来与geom_violin相同,它使用相同的平滑/密度估算算法。