【问题标题】:Create 3 GGplots in 1 PDF within 1 function [duplicate]在 1 个函数内的 1 个 PDF 中创建 3 个 GGplots [重复]
【发布时间】:2013-03-18 09:43:49
【问题描述】:

我想创建一个包含 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 个图,以便将其应用于多个数据集?

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

【问题讨论】:

  • @DidzisElferts 确实我已经读过这个问题了。但是,当仅使用绘图功能时,它可以完美运行。我能以与plot() 完全相同的方式做到这一点吗?我会再看看这个问题,也许我读错了!
  • @DidzisElferts 我发现解决方案确实是 print() 语句!
  • 一个方便的选择是将多个绘图包装到 gridExtra::marrangeGrob(l, t, k) 中,它定义了一种打印方法,并允许您在一页和/或单独的页面上有多个绘图(例如 ncol 和 nrow 设置为 1 )。
  • @baptiste 我会看看那个谢谢你的提示!

标签: r ggplot2


【解决方案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()

【讨论】:

  • 我想在函数中执行此操作的原因是我有 10K + 文件,并且我希望能够随时绘制这 3 个图。但我已经找到了解决方案!最后一次调用应该在print(t + geom.point()) 语句中!但无论如何感谢您的努力
猜你喜欢
  • 2014-11-08
  • 1970-01-01
  • 2014-07-02
  • 2017-05-19
  • 2021-10-23
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多