2015-12-24 60 views
2

我能夠生產具有可變binwidths一個barplot使用在R中,使用ggplot + geom_bar,你如何改變一個條的寬度?

byA<-barplot(table(A),width=B$length, space = 0,col="black") 

barplot with variable binwidths

我怎麼可能做使用ggplot一樣嗎?

我已經試過這樣:

ggplot(data.frame(table(A)), aes(x=B$Start, y=Freq, width=B$length)) + 
    geom_bar(aes(fill=B$length), stat="identity", position="identity") 

enter image description here

我怎樣才能條之間擺脫了空間和改變他們的位置從0開始,而不是在X標記爲中心? (我猜這可能會讓我看到同樣的情節。)注意:我更喜歡第二張圖中的x軸值,所以沒關係。

這些都是前20行我的數據:

bin   Freq 
1 [0,0.78)  9 
2 [0.78,0.99)  1 
3 [0.99,1.07)  1 
4 [1.07,1.201) 1 
5 [1.201,1.211) 0 
6 [1.211,1.77) 3 
7 [1.77,1.95)  0 
8 [1.95,2.14)  2 
9 [2.14,2.15)  0 
10 [2.15,2.581) 0 
11 [2.581,3.04) 4 
12 [3.04,3.11)  0 
13 [3.11,3.22)  0 
14 [3.22,3.33)  1 
15 [3.33,3.58)  3 
16 [3.58,4.18)  8 
17 [4.18,4.29)  2 
18 [4.29,4.48)  4 
19 [4.48,4.62)  4 
20 [4.62,4.8)  3 
+1

嘗試調整寬度 – MLavoie

回答

1

通過使用內stat_binbreaks參數,你可以建立你 正在尋找的陰謀。

# We'll use the diamonds data set within ggplot2 for an example 
library(ggplot2) 

# Set breaks for the bars. The bins are: 
# 1st bin: [0, 100), centered at 50 
# 2nd bin: [100, 500), centered at 300 
# 3rd bin: [500, 1000), 
# 4th bin: [1000, 2000), 
# 5th bin: [2000, 5000), 
# etc. 
# 
# to have (a, b] style intervals use the argument right = TRUE in the stat_bin 
# call. 
brks <- c(0, 100, 500, 1000, 2000, 5000, 7500, 10000, max(diamonds$price)) 

ggplot(diamonds, aes(x = price)) + 
geom_bar() + 
stat_bin(breaks = brks) 

enter image description here

+0

感謝您的輸入 - 它試圖加入stat_bin(符= brks) - 改變brks是我斌左側 - 但我得到的錯誤:stat_bin ()不得與審美一起使用。我知道我必須以錯誤的順序混合變量或包含ggplot的衝突參數,但我沒有看到我的出路。我已經包含了一小部分數據。 – val