【问题标题】:plotting two histograms together [duplicate]将两个直方图绘制在一起[重复]
【发布时间】:2012-06-26 23:28:23
【问题描述】:

可能重复:
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)

【问题讨论】:

标签: r histogram


【解决方案1】:

你想要的那种情节严格来说不是直方图。不过,您可以使用 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)

【讨论】:

    猜你喜欢
    • 2011-04-02
    • 2013-02-10
    • 2015-01-14
    • 2015-05-31
    • 2015-01-10
    • 2017-08-13
    • 2013-10-03
    相关资源
    最近更新 更多