如何在R中将多个堆叠的直方图一起绘制?

时间:2013-11-25 03:11:08

标签: r ggplot2 histogram

我是R的新手(刚开始使用它),我试图在彼此的顶部绘制多个直方图。我看过一些帖子,谈论如何在彼此之上绘制两个直方图,但没有找到任何解释如何做多个的直方图。特别是我的例子,5。

我有5个值,我想绘制叠加的直方图。

a <- c(15.625,12.5,14,15.75,15.375,18.813)

cs <- c(15.875,    7.875,    6.625, 6,  6.5,    9,  7.375,  7.625,  6.875,  7.25,   7.5,    6.75,   6.75,   7.5,    6.75,   6.75,   10.25,  8.625,  8.875,  10.125, 11.75,  11, 16.5,   15.75,  14, 13, 12.5,   13.875, 13.5,   15.25,  17, 15.125, 15.75,  15.25,  13.625, 12.625, 11.375, 12.625, 11.875, 11.375, 16, 18.25,  20.25,  18.125, 22.5,   24.75,  27.5,   28.375, 31.125, 37.75,  32.75,  30.375, 31.875, 33.25,  34.625, 28.5,   28.875, 23.5,   24.625, 22.5,   18, 20.25,  24.875, 26.125, 30.25,  32.375, 36.5,   32.75,  35.25,  28.5,   29, 32.875, 34.5,   34.625, 31.875, 32.375, 29.5,   27.5,   26.875, 28.375, 30.25,  24.75,  26.75,  30.75,  29.625, 35.375, 31.875, 34.875, 40, 44.375, 32.25,  34.25,  35, 43.875, 46.125, 46.625, 29, 22.875, 24.25,  20.125, 25.125, 25.5,   25.875, 24.5,   24.25,  24.5,   24.125, 25, 23, 25, 30.25,  32, 37.5,   37.5,   32, 36.625, 40.625, 40.5,   47, 42.75)

e<- c(10.5,    13.188,  11.125, 10.938, 11.5,   17.625, 24, 18, 15, 17.625, 19.625, 30.5,   32.125)

qr <- c(17.625,    15,  14.875, 17.125, 17.375, 18, 14.75,  18, 17, 17.875, 15.25,  15.5,   23.375, 23.75,  23.125, 20.625, 22.625, 24, 24.125, 22.875, 20.125, 22.25,  25.5,   32.5,   36.375, 39.125, 39, 30.375, 33.375, 35.5,   29, 28.75,  23.875, 32.125, 29.5,   26.75,  36.125, 36.875, 36, 21.375, 21.5,   19.25,  20.25,  24.625, 23, 12.125, 11.625, 12.5,   12.625, 13.5,   12.25,  12.625, 11.5,   11.875, 11.5,   11.25,  11.75,  11.75,  11.5,   10.875, 15, 13.75,  13.25,  13, 16.125, 14, 16.5,   17, 16.75,  15.75,  14.875, 13.625, 12, 13.625, 13.25,  13, 15.25,  14, 13.5,   11.375, 10.375, 9.75,   10.125, 10.25,  13.125, 16.125, 18.875, 17.5,   17.75,  17, 19.625, 20.625, 19.25,  20.375, 20, 27.75,  27.5,   28.5,   28.625, 21.5,   21.5,   21, 20.125, 19.625, 20.125, 21, 25.375, 24.75,  27, 26, 27.125, 25.125, 26.125, 28.875, 30.75,  35, 36.25,  35.875, 34.25,  35.75,  35.375, 28.375, 25.625, 26.375, 26.875, 25.375, 24.75,  29.25,  25.875, 25.75,  24.75,  28.375, 30.875, 35, 35.5,   40.625, 47, 36.625, 38.5,   37.375, 45.875, 23.75,  25.375, 20.5,   23.25,  26.25,  29.5,   30.5,   33.875, 32.5,   28.375, 29.25,  26.5,   26.25,  28.375, 29.875, 36.625, 35, 20.875, 22.125, 23, 23.25,  26.25,  34.25)

w <- c(6.938,    8, 8.25,   7.5,    8,  6.25,   6,  5.25,   4.5,    4.75,   6,  5.875,  6.25,   5,  3.5,    3.25,   4,  3.375,  3.5,    2.75,   3.75,   3.75,   4.75,   4.25,   5,  6,  6,  5.75,   5.625,  5.375,  5,  4.5,    4.375,  3.75,   4.25,   4.375,  4.25,   4.75,   4.625,  4.375,  4.25,   4,  3.75,   3.625,  3.75,   4.5,    6.125,  7,  8.25,   8,  10.375, 10.75,  14.25,  14, 15.5,   27.5,   26.5,   21.75,  25.375, 20.625, 21.75,  17.125, 17.625, 13.75,  14.375, 15.625, 20.75,  20.5,   25.25,  24.375, 20.5,   18.5,   27.25,  33.5,   33, 42.625, 50.125, 47.25,  25.75,  21.25,  21.25,  20.5,   17.625, 16, 15.25,  13.375, 13, 17, 14.125, 16.125, 14.25,  12.25,  14.5,   17.375, 16.125, 15.625, 17.5,   23, 27.625, 28.625, 25.5,   19.75,  19.75,  24.75,  23.625, 24.625, 33.375, 36.25,  36.625, 37.125, 37.625, 37.375, 37.875)

我累了模仿我在thread中找到的代码,但收回了一个矩阵错误。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:11)

你可以用reshape和ggplot2

这样做
require(reshape2)  # this is the library that lets you flatten out data
require(ggplot2)   # plotting library

bucket<-list(a=a,cs=cs,e=e,qr=qr,w=w) # this puts all values in one list

# the melt command flattens the 'bucket' list into value/vectorname pairs
# the 2 columns are called 'value' and 'L1' by default
# 'fill' will color bars differently depending on L1 group
ggplot(melt(bucket), aes(value, fill = L1)) + 
#call geom_histogram with position="dodge" to offset the bars and manual binwidth of 2
geom_histogram(position = "dodge", binwidth=2)

编辑 - 抱歉,您要求堆叠,只需将最后一行更改为:

geom_histogram(position = "stack", binwidth=2)

plot

stacked plot