0

I want to create a .PDF of 2 simple matrices (see below) and 3 simple plots.

The problem I’m experiencing is that if i try to make 3 ggplots within 1 PDF i only get the last plot in the PDF.

I made some examples of both matrices:

testM1<-structure(c(3.97324499399863e-10, 3.4941052533535e-09, 0.000223879747651954, 
0.000317346286270709, 4.95475331444513e-05, 9.37455248795082e-06, 
0.00479174197985962, 0.0117719601999346, 1.95538874649056e-05, 
0.0112286943879835, 0.00938438613835775, 4.94275399906971e-05, 
0.0707123514324416, 0.272635105975824, 0.0017364758608557, 2429999.00716101, 
647545.748442555, 376514.280488326, 357881.804554609, 312416.957044385, 
311751.97469005, 307725.707067659, 290422.886020279, 263015.211836681, 
257847.262909701, 249313.738258419, 236842.707415477, 229077.242836832, 
225838.837415727, 222252.913031706), .Dim = c(15L, 2L), .Dimnames = list(
    NULL, c("tRapAffinities", "score")))

testM2<-structure(c(0.541113046067412, 0.0702310495185636, 6.51934974465011, 
7.32828989739312, 6.15333571096081, 5.41881440001279, 3.90607027852561, 
6.73133704899702, 6.39382638746547, 5.08064000102212, 4.82103531583203, 
5.4283713899347, 6.24047041218676, 8.8615052151427, 5.67248639940994, 
2429999.00716101, 647545.748442555, 376514.280488326, 357881.804554609, 
312416.957044385, 311751.97469005, 307725.707067659, 290422.886020279, 
263015.211836681, 257847.262909701, 249313.738258419, 236842.707415477, 
229077.242836832, 225838.837415727, 222252.913031706), .Dim = c(15L, 
2L), .Dimnames = list(NULL, c("mashAffinities", "score")))

To use the GGPLOT() function i need to convert the matrices in a data.frame (see function)

plotAll<-function(rankedtRapdf = testM1, rankedMashdf = testM2) {
  rankedtRapdf <- as.data.frame(rankedtRapdf)
  rankedMashdf <- as.data.frame(rankedMashdf)
  l<- ggplot()
  l + geom_point(aes(x=rankedtRapdf$tRapAffinities,y=rankedtRapdf$score)) + scale_x_log10() + scale_y_log10()

  t <- ggplot()
  t + geom_point(aes(x=rankedMashdf$mashAffinities,y=rankedMashdf$score), colour = "red")

  k <- ggplot()
  k + geom_point(aes(x=rankedMashdf$mashAffinities,y=log10(rankedtRapdf$tRapAffinities), colour = "darkred"))+
    ggtitle('Measured Mash affinities VS. measured tRap affinities') +
    theme_bw() + labs(x="MASH affinities", y="log10() tRap affinities") +
    theme(axis.title=element_text(face="bold.italic", size="12", color="brown"), legend.position="top")
}

When i try to run this function within a call to pdf() i only get the last plot back.

How can i make those 3 plots at once so that it can be applied on multiple datasets?

p.s. another problem i`m experiencing is that if i convert the matrix to a data.frame within the function i got the following error: Error in eval(expr, envir, enclos) : object 'rankedMashdf' not found

Sander Van der Zeeuw
  • 1,092
  • 1
  • 13
  • 35
  • @DidzisElferts Indeed i already read that question. But when just using the plot function it works perfectly. Am i able to do this in the exact same way as with `plot()` I will have another look at that question maybe i read it wrong! – Sander Van der Zeeuw Mar 18 '13 at 09:50
  • @DidzisElferts i found out that the solution was indeed a print() statement! – Sander Van der Zeeuw Mar 18 '13 at 12:36
  • 1
    One convenient option is to wrap multiple plots into `gridExtra::marrangeGrob(l, t, k)`, which defines a print method and would allow you to have multiple plot on one page and/or separate pages (e.g. ncol and nrow set to 1). – baptiste Mar 18 '13 at 18:18
  • @baptiste I will have a look at that thanks for the hint! – Sander Van der Zeeuw Mar 19 '13 at 09:30

1 Answers1

1

Is there some reason you need to use a function to plot these?

You can do it more simply like this. The option you are looking for is onefile = TRUE.

rankedtRapdf <- as.data.frame(testM1)
rankedMashdf <- as.data.frame(testM2)
l <- ggplot( rankedtRapdf  )+
  geom_point( aes( x = tRapAffinities , y = score ) ) + 
  scale_x_log10() + 
  scale_y_log10()

t <- ggplot( rankedMashdf )+
  geom_point(aes( mashAffinities , y = score ), colour = "red")

k <- ggplot( rankedMashdf ) +
  geom_point( aes( mashAffinities , y = log10( rankedtRapdf$tRapAffinities ), colour = "darkred"))+
  ggtitle('Measured Mash affinities VS. measured tRap affinities') +
  theme_bw() + labs(x="MASH affinities", y="log10() tRap affinities") +
  theme(axis.title=element_text(face="bold.italic", size="12", color="brown"), legend.position="top")



# Print all plots to pdf
pdf( "~/myplots.pdf" , onefile = TRUE )
print(l)
print(t)
print(k)
dev.off()

enter image description here

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • 1
    The reason i want to do it in a function is that i have 10K + files and i want to have the possibility to plot those 3 plots whenever i want. But i already found out the solution! the last call should be within a `print(t + geom.point())` statement! but thanks for the effort anyway – Sander Van der Zeeuw Mar 18 '13 at 12:35