R 在1个函数内以1个PDF格式创建3个GGPlot

R 在1个函数内以1个PDF格式创建3个GGPlot,r,ggplot2,R,Ggplot2,我想创建一个包含2个简单矩阵(见下文)和3个简单图的.PDF文件 我遇到的问题是,如果我试图在1个PDF中绘制3个GGPlot,我只能得到PDF中的最后一个plot 我举了两个矩阵的一些例子: testM1<-structure(c(3.97324499399863e-10, 3.4941052533535e-09, 0.000223879747651954, 0.000317346286270709, 4.95475331444513e-05, 9.37455248795082e-06

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

我遇到的问题是,如果我试图在1个PDF中绘制3个GGPlot,我只能得到PDF中的最后一个plot

我举了两个矩阵的一些例子:

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")))
当我试图在调用
pdf()
的过程中运行此函数时,我只返回最后一个绘图

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


p、 我遇到的另一个问题是,如果我将矩阵转换为函数中的data.frame,我会得到以下错误:eval(expr、envir、enclose)中的错误:找不到对象“rankedMashdf”

是否有理由需要使用函数来绘制这些

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

rankedtrapd@DidzisElferts确实我已经读过这个问题了。但是,只要使用plot函数,它就可以完美地工作。我能用与
plot()完全相同的方法来做这件事吗?
我会再看看这个问题,也许我看错了@DidzisElferts我发现解决方案确实是一个print()语句!一个方便的选择是将多个绘图包装到
gridExtra::marrangeGrob(l,t,k)
,它定义了一种打印方法,并允许您在一页和/或单独的页面上进行多个绘图(例如,ncol和nrow设置为1)。@baptiste I将看到这一点,感谢您的提示!我想在函数中这样做的原因是我有10K+个文件,我希望能够随时绘制这3个绘图。但我已经找到了解决办法!最后一个调用应该在
print(t+geom.point())
语句中!不过还是要谢谢你的努力
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()