如何在R中一起绘制多个堆积的直方图? [英] How to plot multiple stacked histograms together in R?

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

问题描述

我对R非常非常陌生(今天刚开始使用它),并且我试图将多个直方图绘制在彼此之上。我遇到了几篇文章,讨论如何绘制两个直方图,但没有发现任何解释如何做多个。特别是对于我的例子,5。



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



<$ (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, 24 0.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)

我累模仿我在此线程,但收回了一个矩阵错误。任何帮助表示赞赏。

解决方案

您可以使用reshape和ggplot2来完成这项工作。

 require(reshape2)#这是可以让数据变平的库
require(ggplot2)#plotting library

bucket< -list(a = a,cs = cs,e = e,qr = qr,w = w)#将所有值放在一个列表中

#熔化命令将'bucket'列表变为value / vectorname对
#这两列在默认情况下被称为'value'和'L1'
#'fill'将根据L1组的不同而显示不同颜色的条形图
ggplot(melt(bucket),aes(value,fill = L1))+
#CALL geom_histogram与位置= 躲闪 到偏移2
geom_histogram(位置= 躲闪 的杆和手动binwidth,binwidth = 2)

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

  geom_histogram(position =stack,binwidth = 2)




I am very very new to R (just started using it today), and I am trying to plot multiple histograms on top of each other. Ive come across a few posts that talk about how to plot two histograms on top of each other, but haven't found any that explain how to do multiple. Specifically for my example, 5.

I have 5 values that I would like to plot stacked histograms of.

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)

I tired mimicking the code I found in this thread, but got back a matrix error. Any help is appreciated.

解决方案

you can do it like this with reshape and 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)

EDIT - sorry you asked for stacked, just change the last line to:

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

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

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