Skip to Content

multiple ggplot2 in 1 data frame

df

  primer timepoints         mean          sde
   Acan          0 1.000000e+00 0.000000e+00
   Acan         20 9.547922e-01 1.729115e-01
   Acan         40 1.936454e+00 9.934593e-01
   Acan         60 1.261360e+00 2.232165e-01
   Acan        120 2.219807e+00 5.915425e-01
   Acan        240 2.540490e+00 5.651534e-01
   Acan        360 1.518923e+00 1.522455e-01
   Actb          0 1.000000e+00 0.000000e+00
   Actb         20 1.061931e+00 4.362860e-02
   Actb         40 8.835103e-01 1.196449e-01
   Actb         60 8.889279e-01 1.401378e-01
   Actb        120 1.001135e+00 7.770563e-02
   Actb        240 8.551348e-01 1.884853e-01
   Actb        360 7.343955e-01 1.824412e-01

This treats the data like each primer is in 1 df, but I want to make a scatter plot using ggplot2 for each unique primer (the y axis would be column mean and the x axis would be timepoints), could I get lapply to work here?

If I could just lapply a function somehow that would be ideal, a list of plots.

Here's the code I've been using for ggplot, in my attempts to loop this

 plot_gg  <-  function(x){
 
    ggplot(df,aes(x=timepoints,mean)) + 
            geom_point() + 
            geom_line() +
            scale_x_continuous(name='x axis') +
            scale_y_continuous(name='y axis') +
            geom_errorbar(aes(ymin=mean-sde,ymax=mean+sde),width=2) +
            opts(title = primer)    
    }
 
   desired_list <- lapply(unique(df$primer),plot_gg,df)

this is pretty wrong, but, I'm not sure if I should subset the df first according to each individual primer. or if it would be easier to do w/ ggplot in the structure the data is in

if you could help direct me a little bit that would be great