Creating a cumulative step graph in R

前端 未结 3 1513
有刺的猬
有刺的猬 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:50

    There is also the stepfun function in the stats package. Using that, you could use the plot method for that object class:

    sdf <- split(df, individual)
    
    plot(1, 1, type = "n", xlim = c(0, max(events)), ylim = c(0, max(table(individual))),
      ylab = "step", xlab = "time")
    
    sfun <- lapply(sdf, function(x){
        sf <- stepfun(sort(x$events), seq_len(nrow(x) + 1) - 1)
        plot(sf, add = TRUE, col = unique(x$individual), do.points = FALSE)
    })
    

    enter image description here

提交回复
热议问题