How subset a data frame by a factor and repeat a plot for each subset?

前端 未结 3 433
天涯浪人
天涯浪人 2020-11-27 04:53

I am new to R. Forgive me if this if this question has an obvious answer but I\'ve not been able to find a solution. I have experience with SAS and may just be thinking of t

3条回答
  •  庸人自扰
    2020-11-27 05:42

    A few years ago, I wanted to do something similar - plot individual trajectories for ~2500 participants with 1-7 measurements each. I did it like this, using plyr and ggplot2:

    library(plyr)
    library(ggplot2)
    
    d_ply(dat, .var = "participant_id", .fun = function(x) {
    
        # Generate the desired plot
        ggplot(x, aes(x = phase, y = result)) +
            geom_point() +
            geom_line()
    
        # Save it to a file named after the participant
        # Putting it in a subdirectory is prudent
        ggsave(file.path("plots", paste0(x$participant_id, ".png")))
    
    })
    

    A little slow, but it worked. If you want to get a sense of all participants' trajectories in one plot (like your second example - aka the spaghetti plot), you can tweak the transparency of the lines (forget coloring them, though):

    ggplot(data = dat, aes(x = phase, y = result, group = participant_id)) + 
        geom_line(alpha = 0.3)
    

提交回复
热议问题