在1个函数中创建1个PDF中的3个GGplots

时间:2013-03-18 09:43:49

标签: r ggplot2

我想创建一个包含2个简单矩阵的.PDF(见下文)和3个简单的图。

我遇到的问题是,如果我尝试在1个PDF中制作3个ggplots,我只会获得PDF中的最后一个情节。

我做了两个矩阵的例子:

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")))

使用GGPLOT()函数我需要转换data.frame中的矩阵(参见函数)

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")
}

当我尝试在pdf()的调用中运行此函数时,我只能回到最后一个情节。

如何一次制作这3个图,以便它可以应用于多个数据集?

P.S。我遇到的另一个问题是,如果我将矩阵转换为函数中的data.frame,我会收到以下错误:eval(expr,envir,enclos)中的错误:找不到对象'rankingMashdf'

1 个答案:

答案 0 :(得分:1)

是否有某些原因需要使用函数来绘制这些?

你可以更简单地这样做。您要查找的选项是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