0

I have the following example data:

my.list <- vector('list',1000)
for(i in 1:1000)
{
    temp <- sample(c("type1","type2"),1)
    my.list[[i]] <- data.frame(time=i,type=temp)
}
df <- do.call('rbind',my.list)

I want to plot the variation of the type variable with time. I used the following:

ggplot(df,aes(x=time,y=type)) + geom_line()

with this command, I am not getting the expected result:

enter image description here

Notice how a transition from type 1 to type 2 and vice versa doesn't show in the plot. Did I miss something ?

Plus, in this plot, it seems that at time x, the type variable takes both type1 and type2 as values which is contradictory to the data frame's contents

Imlerith
  • 479
  • 1
  • 7
  • 15

1 Answers1

2

For this two work, you have to use the group argument.

ggplot(df,aes(x=time,y=type, group=1)) + geom_line()

Note, however, that the result will be hard to interpret since the lines are quite dense when using 1000 observations. If you use only 100 observations, so

set.seed(1)
my.list <- vector('list',100)
for(i in 1:100)
{
  temp <- sample(c("type1","type2"),1)
  my.list[[i]] <- data.frame(time=i,type=temp)
}
df <- do.call('rbind',my.list)

the result looks as follows:

enter image description here

Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22
  • this seems to work, I'll just put a slider to let the user choose the range. I will look up what "group" does exactly – Imlerith Jul 16 '16 at 15:50