2

我可以使用 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)).**
4

1 回答 1

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))

在此处输入图像描述


编辑:根据您的新问题,在您的设备上尝试这个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))
于 2014-10-03T07:40:38.450 回答