2016-09-23 187 views
0

我需要繪製直方圖,但我的數據處於類間隔中。R中類間隔的直方圖

以下是我的劣勢和優越性。

xinf=c(0,10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5) 
xsup=c(10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5,30) 

頻率:

Fo=c(2,5,16,42,69,51,32,23,9,1) 

我不知道如何繪製的直方圖與此數據。

回答

0

試着這麼做

hist(rep((xinf+xsup)/2, Fo), breaks = union(xinf,xsup), 
    xlab = 'Variable name', main='Histogram') 

也許我會被

xinf=c(9.5,10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5) 
xsup=c(10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5,19.5) 
0

這裏改變變量xinfxsup有一種方法:

# data 
Fo=c(2,5,16,42,69,51,32,23,9,1) 
xinf=c(0,10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5) 

# breaks for histogram bars 
breaks = c(xinf, 30) 

# make a histogram with temp data 
h = hist(rep(1, length(Fo)), breaks = xx, plot = FALSE) 
# Fill the density and counts manually 
h$density = Fo/sum(Fo) 
h$counts = Fo 
plot(h, freq = TRUE) 

請注意,freq = TRUE部分是必要的得到y軸的數量。它還提供了有關這些區域的警告消息。另外,對於密度估算,您可以使用比我使用的更復雜的東西。

2

因爲你的數據已經被彙總了,所以我不會做出「正確的」直方圖。相反,我們可以用柱狀圖假的吧:

barplot(Fo, width = xsup-xinf) 

enter image description here

一個細微之處是,因爲它是一個柱狀圖中,有條之間的間隔。

barplot(Fo, width = xsup-xinf, space = 0) 

enter image description here

或其他一些接近零值:這可以通過被刪除。其他可選組件:axis(1),barplot(..., main="My Bars")

enter image description here

至於@EdwardCarney開始建議,你可以提煉這樣的軸:

barplot(Fo, width = xsup-xinf, space = 0, main = "My Bars") 
lbls <- sort(union(xinf, xsup)) 
axis(1, labels = lbls, at = lbls, las = 2) 

enter image description here

(他的建議是傾向於倉的中心,在這裏我選擇無論哪種方式,你都可以選擇注意,如果你沒有設置space=0那麼你必須調整標籤的位置,因爲所有的東西都會被分開。)

+0

我也喜歡這些數據的barplot。我使用'names.arg'參數添加了類間隔的中心,如下所示:'barplot(Fo,las = 1,names.arg = as.character((xsup-xinf)/ 2 + xinf),ylim = c (0,75))' –