2012-06-05 44 views
3

用下面的代碼,我得到一個直方圖如下使堆疊矩形,而不是列的直方圖

x <- rnorm(100) 
hist(x,col="gray") 

我能做些什麼來得到通過他們的輪廓顯示條爲層疊矩形(可見的,而不是填充顏​​色的變化)而不是統一的列?每個矩形表示一個頻率,例如1,儘管我希望能夠通過參數來改變它。

histogram

+0

你也許能夠適應'plotrix :: symbolbarplot'這樣做... –

+2

相關問題:http://stackoverflow.com/questions/9527802/is-it-possible-to-split-bars-in-barplot-with-r –

+0

@VincentZoonekynd我' d更喜歡控制顏色,但是,您的解決方案在很大程度上解決了我的問題。 – user1202664

回答

0

這裏是讓你開始一個函數(它實際上是在TeachingDemos包爲tkBrush功能的示例部分modicication):

 rechist <- function(x,...){ 
     tmp <- hist(x,plot=F) 
     br <- tmp$breaks 
     w <- as.numeric(cut(x,br,include.lowest=TRUE)) 
     sy <- unlist(lapply(tmp$counts,function(x)seq(length=x))) 
     my <- max(sy) 
     sy <- sy/my 
     my <- 1/my 
     sy <- sy[order(order(x))] 
     plot.new() 
     plot.window(xlim=range(br), ylim=c(0,1)) 
     rect(br[w], sy-my, br[w+1], sy, 
      border=TRUE, col='grey') 
     rect(br[-length(br)], 0, br[-1], tmp$counts*my) 
     axis(1) 
    } 

rechist(iris$Petal.Length) 
2

從在this question(H/T文森特Zoonekynd)的回答。

x <- rnorm(100) 
hist(x,col="gray") 
abline(h=seq(5,40,5),col="white")