可能重复:
如何在R中一起绘制两个直方图?
我想将两个直方图绘制在一起,它们都具有相同的x轴单位和y轴单位.两个直方图取自两个文件,inp1和inp2.我尝试了以下代码,但它无法正常工作:
x1<-hist(inp1, 120, plot = 0)
x2<-hist(inp2, 120, plot = 0)
hist(x1, x2, 240, plot = 1)
Run Code Online (Sandbox Code Playgroud)
你想要的那种情节严格来说不是直方图.您可以创建类似的东西,不过,使用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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10569 次 |
| 最近记录: |