绘制连续变量的两个直方图,其中条彼此相邻而不是重叠

时间:2012-10-08 15:37:04

标签: r plot histogram

我试图在一个图中绘制两个直方图,但这两个组的分布方式使直方图难以解释。我的直方图现在看起来像这样:

enter image description here

这是我的代码:

hist(GROUP1, col=rgb(0,0,1,1/2), breaks=100, freq=FALSE,xlab="X",main="")  # first histogram
hist(GROUP1, col=rgb(1,0,0,1/2), breaks=100, freq=FALSE , add=T)  # second
legend(0.025,600,legend=c("group 1","group 2"),col=c(rgb(1,0,0,1/2),rgb(0,0,1,1/2)),pch=20,bty="n",cex=1.5)

是否可以绘制这个直方图,两组的条彼此相邻,而不是重叠?我意识到这可能会增加一些混乱,因为X轴代表一个连续变量......其他关于如何使这个情节更清晰的建议当然也是受欢迎的!

2 个答案:

答案 0 :(得分:8)

不是搞乱重叠的直方图,而是:

  1. 在单独的面板中有两个直方图,即

    par(mfrow=c(1,2))
    d1 = rnorm(100);d2 = rnorm(100);
    hist(d1);hist(d2)
    
  2. 或者,使用密度图

    plot(density(d1))
    lines(density(d2), col=2)
    
  3. 或使用密度图和直方图的组合

    hist(d1, freq=FALSE)
    lines(density(d2), col=2)
    

答案 1 :(得分:6)

您可能会误导barplot

multipleHist <- function(l, col=rainbow(length(l))) {
    ## create hist for each list element
    l <- lapply(l, hist, plot=FALSE);

    ## get mids
    mids <- unique(unlist(lapply(l, function(x)x$mids)))

    ## get densities
    densities <- lapply(l, function(x)x$density[match(x=mids, table=x$mids, nomatch=NA)]);

    ## create names
    names <- unique(unlist(lapply(l, function(x)x$breaks)))

    a <- head(names, -1)
    b <- names[-1]
    names <- paste("(", a, ", ", b, "]", sep="");

    ## create barplot list
    h <- do.call(rbind, densities);

    ## set names
    colnames(h) <- names;

    ## draw barplot
    barplot(h, beside=TRUE, col=col);

    invisible(l);
}

示例:

x <- lapply(c(1, 1.1, 4), rnorm, n=1000)
multipleHist(x)

multiple histograms in one plot

修改 这是一个像OP建议的那样绘制x轴的例子。恕我直言,这是非常误导的(因为条形图的箱子不是连续的值),并且应该使用。

multipleHist <- function(l, col=rainbow(length(l))) {
    ## create hist for each list element
    l <- lapply(l, hist, plot=FALSE);

    ## get mids
    mids <- unique(unlist(lapply(l, function(x)x$mids)))

    ## get densities
    densities <- lapply(l, function(x)x$density[match(x=mids, table=x$mids, nomatch=NA)]);

    ## create names
    breaks <- unique(unlist(lapply(l, function(x)x$breaks)))

    a <- head(breaks, -1)
    b <- breaks[-1]
    names <- paste("(", a, ", ", b, "]", sep="");

    ## create barplot list
    h <- do.call(rbind, densities);

    ## set names
    colnames(h) <- names;

    ## draw barplot
    barplot(h, beside=TRUE, col=col, xaxt="n");

    ## draw x-axis
    at <- axTicks(side=1, axp=c(par("xaxp")[1:2], length(breaks)-1))
    labels <- seq(min(breaks), max(breaks), length.out=1+par("xaxp")[3])
    labels <- round(labels, digits=1)
    axis(side=1, at=at, labels=breaks)

    invisible(l);
}

multiple histograms in one plot (modified x-axis)

请在github上找到完整的源代码。