绘制数据框中各种数组的CDF图

时间:2014-10-03 07:19:46

标签: r ggplot2 cdf

我可以使用

绘制3个数据系列的累积分布图
library(ggplot2)

a1 <- rnorm(1000, 0, 3)
a2 <- rnorm(1000, 1, 4)
a3 <- rnorm(800, 2, 3)

    df <- data.frame(x = c(a1, a2, a3), ggg=factor(rep(1:3, c(1000,1000,800))))
    ggplot(df, aes(x, colour = ggg)) + 
      stat_ecdf()+
      scale_colour_hue(name="my legend", labels=c('AAA','BBB', 'CCC'))

但现在我有大约100个观察到的数据,例如a1,a2 ...... a100,有5000行,我想要累计分布图,但我不想使用循环,而是我想使用像apply或tapply和ggplot包。

**sample data :df = data.frame(matrix(rnorm(20), nrow=5000,ncol=100)).**

1 个答案:

答案 0 :(得分:0)

您可以尝试使用ls mget组合,例如

a1 <- rnorm(1000, 0, 3)
a2 <- rnorm(1000, 1, 4)
a3 <- rnorm(800, 2, 3)
a100 <- rnorm(800, 2, 3) # <- adding some more vectors
a200 <- rnorm(800, 2, 3) # <- adding some more vectors 
a300 <- rnorm(800, 2, 3) # <- adding some more vectors 
a1000 <- rnorm(800, 2, 3) # <- adding some more vectors

temp <- mget(ls(pattern = "^a\\d+$"))
df <- data.frame(x = unlist(temp), ggg = factor(rep(seq_len(length(temp)), sapply(temp, length))))
ggplot(df, aes(x, colour = ggg)) + 
  stat_ecdf()+
  scale_colour_hue(name="my legend", labels=names(temp))

enter image description here


修改:根据您的新问题,请在df上尝试此问题(在提供的df上看起来不太好,因为所有值都相同列)

library(reshape2)
df2 <- melt(df)
df2$x <- rep(seq_len(nrow(df)), ncol(df))
ggplot(df2, aes(x, value, color = variable)) + 
  stat_ecdf()+
  scale_colour_hue(name="my legend", labels=names(df))