2012-06-25 72 views
-1

可能重複:
How to plot two histograms together in R?繪製兩個柱狀圖一起

我想繪製兩個柱狀圖在一起,兩者具有相同的X軸的單位和y軸單位。兩個直方圖取自兩個文件inp1和inp2。我試過下面的代碼,但它不工作:

x1<-hist(inp1, 120, plot = 0) 
x2<-hist(inp2, 120, plot = 0) 
hist(x1, x2, 240, plot = 1) 
+0

你想繪製他們彼此,或在單獨面板的頂部覆蓋? –

+0

不在另一個之上 – newbie555

+2

@rockluke那麼你爲什麼不加入更多的解釋來描述你想要的情節。 – Dason

回答

9

你想要的那種情節並不嚴格地說是直方圖。你可以喜歡它創造的東西,不過,使用barplot()beside=TRUE

## Example data 
d1 <- rnorm(1000) 
d2 <- rnorm(1000, mean=1) 

## Prepare data for input to barplot 
breaks <- pretty(range(c(d1, d2)), n=20) 
D1 <- hist(d1, breaks=breaks, plot=FALSE)$counts 
D2 <- hist(d2, breaks=breaks, plot=FALSE)$counts 
dat <- rbind(D1, D2) 
colnames(dat) <- paste(breaks[-length(breaks)], breaks[-1], sep="-") 

## Plot it 
barplot(dat, beside=TRUE, space=c(0, 0.1), las=2)