2016-07-22 65 views
0

Minitab中,很容易創建「分割點」直方圖。這可以在ggplot2完成嗎?ggplot2中的分界點直方圖

例如,

df <- data.frame(x = c(0.08, 0.21, 0.25, 0.4, 0.5, 0.6)) 
ggplot(df, aes(x = x)) + geom_histogram(binwidth = 0.1) 

enter image description here

正如你可以在這裏看到,R默認爲 「中點」 直方圖。包含0.08的酒吧在0.1處標有酒吧。包含0.210.25的欄正在標記爲0.2等等。

我可以以某種方式改變這些酒吧因此第一條涵蓋00.1之間的區域,而第二條涵蓋0.20.3,等等之間的區域?

+2

不知道你想要輸出什麼,但這樣做嗎? 'ggplot(df,aes(x = x))+ geom_histogram(breaks = seq(0,max(df $ x),by = 0.1))' –

+0

邊界參數也應該在這裏: 'ggplot(df,aes (x = x))+ geom_histogram(binwidth = 0.2,boundary = 1)' – pHroc

回答

1

您可以通過兩種方式擺脫問題:使用參數「中心」或使用「邊界」。使用「center」可以指定其中一個元素的中心,「boundary」類似,但是您可以指定兩個元素之間邊界的值。值得注意的是,「中心」和「邊界」可以高於或低於數據範圍,在這種情況下,所提供的值將被移動足夠的寬度數量。

在這種情況下,你已經知道了垃圾桶,它的邊界的寬度,所以用這個參數,你可以很容易地做你問:

library(ggplot2) 
df <- data.frame(x = c(0.08, 0.21, 0.25, 0.4, 0.5, 0.6)) 

# This is "center" solution: 
ggplot(df, aes(x = x)) + geom_histogram(binwidth = 0.1, center=0.05) 

# This is with "boundary" parameter 
ggplot(df, aes(x = x)) + geom_histogram(binwidth = 0.1, boundary=0.1) 

您可以找到參考的細節和更多信息geom_histogram 。 希望這可以幫助