2

i can plot cummulative distribution plots for 3 data series using

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

but now i have around 100 observed data for example a1,a2 ......a100 with 5000 rows and i want cummulative distribution plots all together but i dont want to use loop rather i want to use functions like apply or tapply and ggplot package.

**sample data :df = data.frame(matrix(rnorm(20), nrow=5000,ncol=100)).**
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
saurabh
  • 391
  • 1
  • 2
  • 11

1 Answers1

0

You could try using ls mget combination, for example

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


Edit: Per your new question, try this on your df (it won't look so good on the provided df because all the values are equal in all the columns)

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))
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • hey @David thanks a lot can u explain me temp <- mget(ls(pattern = "^a\\d+$")) line in yor code as i never used the function mget. – saurabh Oct 03 '14 at 07:48
  • `mget` is pulling all the data frames from the global environment that were specified in `ls` using the regex expression. If you don't know regex, try typing `?regex` in the console. This expression is very basic. It searches for "a" at the beginning of the data frames names, afterwords it makes sure there are numbers following it and `$` makes sure there is nothing coming after these numbers. I'm doing these restrictions in order to avoid other objects in the global environment being pulled by `mget` too. – David Arenburg Oct 03 '14 at 07:54
  • i have my variables inside a data frame name as V1,V2 .... V100 and the data frame name is "dd" i attached the dataframe and then tried the function but i geth the following error : Don't know how to automatically pick scale for object of type zoo. Defaulting to continuous Error: Aesthetics must either be length one, or the same length as the dataProblems:ggg – saurabh Oct 03 '14 at 08:11
  • This will be another question then. You will have to use `melt` from `reshape2` package – David Arenburg Oct 03 '14 at 08:44
  • the above plot does not work for the data frame it produces just a single plot rather than 100 different curves for each variable in a data frame – saurabh Oct 03 '14 at 11:09
  • I told you, your data frame has exactly the same values in each column, thus all the lines are plotted over each other and it looks the same – David Arenburg Oct 03 '14 at 11:10
  • no i tried with a different data frame again i get the same line ,however i can get a different plot from matlab(what i wanted) ,but not from r,i guess there is something wrong. – saurabh Oct 03 '14 at 11:20
  • Please provide a reproducible example. I can't know what didn't work without it – David Arenburg Oct 04 '14 at 18:48
  • please try with this example as i am using a totally random data even for that i get a single plot ,df = data.frame(matrix(rnorm(20), nrow=5000,ncol=100)) there should be 100 plots for this. thanks – saurabh Oct 04 '14 at 19:07
  • This data set contains exactly the same values in each column, can't you see it? Do `View(df)` – David Arenburg Oct 04 '14 at 19:11