在R中的单个pdf文档中保存多个ggplot?

水电

有没有一种方法可以将全部保存plots为单个pdf文档

library(tidyverse)
library(lubridate)

set.seed(123)

DF1 <- data.frame(Date = seq(as.Date("2001-01-01"), to= as.Date("2003-12-31"), by="day"),
                  A = runif(1095, 0,10),
                  D = runif(1095,5,15))
DF1 %>% pivot_longer(names_to = "Variable", values_to = "Value", -Date) %>% 
  ggplot(aes(x = Date, y = Value))+
  geom_line()+
  facet_wrap(~Variable)+
  ggsave("Plot1.pdf", dpi = 200, height = 6, width = 8)


DF2 <- data.frame(Date = seq(as.Date("2005-03-01"), to= as.Date("2005-05-31"), by="day"),
                  Z = runif(92, 0,10))

DF2 %>% ggplot(aes(x = Date, y = Z))+
  geom_line()+
  ggsave("Plot2.pdf", dpi = 200, height = 6, width = 8)

对于您的数据,您可以保存绘图,然后使用pdf如果您有很多地块,则最好使用地块创建一个列表并将其导出。这是您的绘图的代码(您可以使用的height和width参数进行播放pdf):

library(tidyverse)
library(lubridate)

set.seed(123)

DF1 <- data.frame(Date = seq(as.Date("2001-01-01"), to= as.Date("2003-12-31"), by="day"),
                  A = runif(1095, 0,10),
                  D = runif(1095,5,15))
DF1 %>% pivot_longer(names_to = "Variable", values_to = "Value", -Date) %>% 
  ggplot(aes(x = Date, y = Value))+
  geom_line()+
  facet_wrap(~Variable) -> G1

DF2 <- data.frame(Date = seq(as.Date("2005-03-01"), to= as.Date("2005-05-31"), by="day"),
                  Z = runif(92, 0,10))

DF2 %>% ggplot(aes(x = Date, y = Z))+
  geom_line()->G2
#Export
pdf('Yourfile.pdf',height = 6, width = 8)
plot(G1)
plot(G2)
dev.off()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章