简体   繁体   English

带R的堆积累积直方图/面积图

[英]Stacked cumulative histograms/area plot with R

I have some data which looks like 我有一些看起来像的数据

library(data.table)
set.seed(1)
data <- data.table( sample(1:6,1000,replace=T), rnorm(1000, mean = 5))
     V1       V2
   1:  2 5.077303
   2:  3 4.703131
   3:  4 3.816758
   4:  6 5.011293
   5:  2 5.991601
  ---            
 996:  5 5.114406
 997:  4 4.607735
 998:  2 6.652358
 999:  2 3.756302
1000:  2 4.088074

I want to create a plot with some sort of cumulative histograms or cumulative area graph filled by V1 and with V2 on the x-axis, stacked on top of each other. 我想创建一个绘图,该绘图具有某种形式的累积直方图或累积面积图,由V1填充,x轴上的V2相互堆叠。 The result should look something like this: 结果应如下所示:

http://i.imgur.com/GGywhzu.png http://i.imgur.com/GGywhzu.png

This is what I tried so far: 这是我到目前为止尝试过的:

library(ggplot2)
ggplot(data, aes(group=V1, x = V2, fill=as.factor(V1))) +
    geom_histogram(aes(y=cumsum(..count..)/sum(..counts..)), bins = 50)

However it does not stack the data correctly onto each other. 但是,它不能正确地将数据彼此堆叠。 It would also be nicer with an area plot, instead of the histogram bars. 用面积图代替直方图条也更好。 This is what the above code produces: plot 这就是上面的代码产生的:

is that what you're looking for? 那是您要找的东西吗?

data$V1 <- as.factor(data$V1)
ggplot(data,aes(group=V1,fill=V1,x=V2))+
  geom_histogram()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM