2017-05-29 103 views
0

我试图解释小提琴图表给我的老板,我想用geom_freqpoly,所以我可以说“小提琴图只是一个柱状图翻转在其侧面和镜像”:如何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

换句话说,小提琴情节抚平的zeroe s,但是直方图不是。 如何通过使geom_freqpoly平滑零点来使两个图表匹配?

回答

2

这实际上并不是geom_freqpoly的目的,它代表了每个值的实际频率。对于平滑,你想geom_density

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

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

+0

你可以添加'+ coord_flip()'来说明“翻转和镜像”。 – neilfws

+0

@Marius我认为我需要同时使用两个:第一个'geom_freqpoly'向他展示计数,然后'geom_density'向前靠近小提琴。我接受你的答案。 – lebelinoz

+0

@neilfws好的提示,但我认为我的老板能够想象一个翻转的图表。 – lebelinoz