Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

ggplot2 - How to plot multiple stacked histograms together in R?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

plot

stacked plot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...