-1

可能重复:
如何在 R 中同时绘制两个直方图?

我想一起绘制两个直方图,它们都具有相同的 x 轴单位和 y 轴单位。两个直方图取自两个文件,inp1 和 inp2。我已经尝试了以下代码,但它不起作用:

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

1 回答 1

9

你想要的那种情节严格来说不是直方图。barplot()不过,您可以使用with创建类似的东西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)
于 2012-06-25T18:30:58.597 回答