2017-05-29 16 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