2012-06-25 68 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)