1

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

1 Answer 1

1

I think the missing pieces are a need to redo the definition of arguments to geom_errorbar and add the use of facet_wrap. If you specify the number of columns and rows in the layout of facet_warp you can get multiple pages. Another way to print multiple pages is with the grid::grid.newpage() function.

 ggplot(df, aes(x = timepoints, y = mean, ymin = mean - sde, 
    ymax = mean + sde)) + 
 geom_errorbar() + geom_point() + geom_line() + 
 facet_wrap(~ primer) + 
 xlab('x axis') + ylab('y axis') + opts(title = "primer")

For the multi-page request added in the comment below and using @Thierry's edits:

pdf("twopage.pdf", onefile=TRUE)
   for ( i in unique(df$primer) ) { 
       g <- ggplot(df[df$primer == i, ], aes(x = timepoints, y = mean, ymin = mean - sde, 
       ymax = mean + sde)) + 
       geom_errorbar() + geom_point() + geom_line() + 
       facet_wrap(~ primer, ncol=1, nrow=1) + 
       xlab('x axis') + ylab('y axis') + opts(title = "primer") 
       print(g) ; cat(paste("printing", i, "\n"))}
   dev.off()
Sign up to request clarification or add additional context in comments.

5 Comments

This is producing every single plot onto one page, whereas I wanted one plot per page, or just a list of plots
if there are 44 primers lets say facet_wrap(~ primer, ncol=4, nrow=2) this won't work, how can i let the remainder not fill the page necessarily
If you post a question that fully describes the complexity of your problem you are more likely to get a satisfying answer.
thank you very much, I hate to bother with another question, but is there an easy way to just use lapply and make a list of plots, w/o printing to a pdf or anything, just a list, each element being one primer's plot?
You can skip the pdf call and then the plots will get printed on top of each other onto the same interactive device .... or you can create new plot windows before each print call with dev.new().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.