R 在数据帧中绘制各种阵列的CDF图

R 在数据帧中绘制各种阵列的CDF图,r,ggplot2,cdf,R,Ggplot2,Cdf,我可以使用 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()+

我可以使用

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

例如,您可以尝试使用
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))

嘿@David非常感谢你能解释一下吗temp
mget
正在使用正则表达式从全局环境中提取
ls
中指定的所有数据帧。如果您不知道regex,请尝试在控制台中键入
?regex
。这个表达非常基本。它在数据帧名称的开头搜索“a”,在其后搜索,确保后面有数字,
$
确保后面没有数字。我做这些限制是为了避免全局环境中的其他对象也被
mget
拉入。我的变量位于数据帧名称中,如V1、V2。。。。V100,数据帧名称为“dd”。我附加了数据帧,然后尝试了该函数,但出现以下错误:不知道如何为zoo类型的对象自动拾取比例。默认为连续错误:美学的长度必须为1,或者与数据问题的长度相同:GGG那么这将是另一个问题。您必须使用
melt
from
restrape2
package上述绘图不适用于数据框,它只生成一个绘图,而不是数据框中每个变量的100条不同曲线
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))
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))