Creating a cumulative step graph in R

前端 未结 3 1524
有刺的猬
有刺的猬 2020-12-18 08:34

Say I have this example data frame

set.seed(12345)
n1 <- 3
n2 <- 10
n3 <- 60

times <- seq(0, 100, 0.5)

individual <- c(rep(1, n1), 
                 


        
3条回答
  •  一生所求
    2020-12-18 08:54

    Use ggplot2:

    library(ggplot2)
    
    # Add step height information with sequence and rle
    df$step <- sequence(rle(df$individual)$lengths)
    
    # plot
    df$individual <- factor(df$individual)
    ggplot(df, aes(x=events, group=individual, colour=individual, y=step)) + 
      geom_step()
    

    enter image description here

提交回复
热议问题