簡體   English   中英

如何使用 ggplot2 在多個 pdf 頁面中獲取繪圖

[英]How to get plots in several pdf pages using ggplot2

我需要幫助將圖形處理成多個 pdf 頁面。 這是我當前的代碼:

file <- read.csv(file="file.csv") 
library(ggplot2)
library(gridExtra)
library(plyr)

gg1 <- ggplot() +
  geom_line(aes(x=TIME, y=var1, colour = "z1"), file) +
  geom_line(aes(x=TIME, y=var2, colour = "z2"), file) + 
  geom_point(aes(x=TIME, y=var3), file) + facet_wrap( ~ ID, ncol=5)+ 
  xlab("x") +
  ylab("Y") +
  ggtitle(" x ") + scale_colour_manual(name="Legend",
    values=c(z1="red", z2 ="blue")) + theme(legend.position="bottom")   
gg10 = do.call(marrangeGrob, c(gg1, list(nrow=4, ncol=4)))
ggsave("need10.pdf", gg10)

這是創建的圖像,沒有拆分我的圖像

在此處輸入圖片說明

我希望有一個代碼來在多個頁面中以 4 x 4 布局獲取我的圖。 我的代碼的最后兩行需要調整,我不知道如何自己修復。

ggplus包裝器似乎ggplus您的需求。 我從您的原始代碼塊中更改了以下代碼塊中的一些內容: facet_wrap被注釋掉,並且file被移動到ggplot以便不必在每個geom_*重新指定geom_*

gg1 <- ggplot(file) +
  geom_line(aes(x=TIME, y=var1, colour = "z1")) +
  geom_line(aes(x=TIME, y=var2, colour = "z2")) + 
  geom_point(aes(x=TIME, y=var3)) +
  # facet_wrap( ~ ID, ncol=5) +
  xlab("x") +
  ylab("Y") +
  ggtitle(" x ") + 
  scale_colour_manual(name="Legend",
    values=c(z1="red", z2 ="blue"),
    labels=c("X","Y")) +
  theme(legend.position="bottom")   

devtools::install_github("guiastrennec/ggplus")
library(ggplus)
pdf("need10.pdf")
gg10 <- facet_multiple(plot=gg1, facets="ID", ncol = 4, nrow = 4)
dev.off()

在此處輸入圖片說明 在此處輸入圖片說明

4年后...

由於ggplus棄用,您可以考慮使用ggforce 您可以使用文檔中描述的任何相關facet_*選項。 例如, facet_matrix

# Standard use:
ggplot(mpg) +
  geom_point(aes(x = .panel_x, y = .panel_y)) +
  facet_matrix(vars(displ, cty, hwy))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM