一起绘制两个直方图 [英] plotting two histograms together

查看:261
本文介绍了一起绘制两个直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Possible Duplicate:
How to plot two histograms together in R?

我想一起绘制两个直方图,它们都具有相同的x轴单位和y轴单位.从两个文件inp1和inp2中获取两个直方图.我已经尝试了以下代码,但无法正常工作:

I want to plot two histograms together, both of them having same x axis units and y axis units. Two histograms are taken from two files, inp1 and inp2. I have tried the following code but it is not working:

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

推荐答案

您想要的绘图类型严格来说不是直方图.不过,您可以将barplot()beside=TRUE一起使用:

The kind of plot you want is not strictly speaking a histogram. You can create something like it, though, using 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)

这篇关于一起绘制两个直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆