0

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

我遇到的问题是,如果我尝试在 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 个图,以便它可以应用于多个数据集?

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

4

1 回答 1

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

在此处输入图像描述

于 2013-03-18T11:35:13.633 回答