Printing multiple ggplots into a single pdf, multiple plots per page
This solution is independent of whether the lengths of the lists in the list p
are different.
library(gridExtra)
pdf("plots.pdf", onefile = TRUE)
for (i in seq(length(p))) {
do.call("grid.arrange", p[[i]])
}
dev.off()
Because of onefile = TRUE
the function pdf
saves all graphics appearing sequentially in the same file (one page for one graphic).
plotting multiple ggplots in a several page pdf (one or several plots per page)
The solution was actually pretty simple in the end...
### create a layout matrix (nrow and ncol will do the trick too, but you have less options)
layout_mat<-rbind(c(1,1,2),
c(1,1,3))
plots<-marrangeGrob(plot.list, layout_matrix=layout_mat)
ggsave( filename="mypdf.pdf", plots, width=29.7, height=21, units="cm")
This version actually gives you full control over plot sizes and uses the entire page!
Putting multiple plots on each page of a large pdf file
Edited response. Is this what you were after? If not please explain further.
This seems like a case where bringing the data in the proper format is much easier than fiddling with ggplot. Once the data is in the long format instead of wide format the for loop is not necessary anymore.
This code produces the following graph:
library(tidyverse)
raw_data = read_delim("stackoverflowdata.csv", col_names = TRUE, delim = ";") %>%
gather(compound, value, -Group)
ggplot(raw_data,aes(x=Group, y =value)) +
geom_boxplot(na.rm = TRUE) +
facet_wrap(vars(compound), scales="free_y") +
labs(y = 'Relative Intensity') +
theme(axis.text.x = element_text(size = 8, angle = 45, hjust = 1 ))
Saving multiple ggplots from ls into one and separate files in R
It's best to have your plots in a list
l = mget(plots)
Then you can simply print them page-by-page,
pdf("all.pdf")
invisible(lapply(l, print))
dev.off()
or save one plot per file,
invisible(mapply(ggsave, file=paste0("plot-", names(l), ".pdf"), plot=l))
or arrange them all in one page,
# On Windows, need to specify device
ggsave("arrange.pdf", arrangeGrob(grobs = l), device = "pdf")
or arrange them 2x2 in multiple pages,
# need to specify device on Windows
ggsave("arrange2x2.pdf", marrangeGrob(grobs = l, nrow=2, ncol=2),
device = "pdf")
etc.
(untested)
saving multiple ggplot in a single pdf documents in R?
For your data, you could save the plots and then using pdf
. If you have plenty of plots it would be better to create a list with the plots and the export them. Here the code for your plots (You can play with height and width parameters of 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()
ggplot: Save multiple plots in one pdf with the same plot proportions
Solution 1:
Add newlines (\n
) to ylab
(i.e. ylab("module\n\n\n\n\n")
). Like this you will increase space between axis title and plot.
a <- ggplot() +
geom_point(data = perWorkField, mapping = aes(x = percent, y = module)) +
xlim(c(0, 100)) +
ylab("module\n\n\n\n\n")
Solution 2:
Very dirty hack: add white space to your text. Problem with my solution is that you have to adjust nWhiteSpace
manually (ie., it's not equal to nchar("ThisIsALongText")
).
nWhiteSpace <- 24
foo <- paste(c(rep(" ", nWhiteSpace), "A"), collapse = "")
foo
[1] " A"
df1 <- perWorkField %>% mutate(module = replace(module, module == "A", foo))
a <- ggplot() +
geom_point(data = df1, mapping = aes(x = percent, y = module)) +
xlim(c(0, 100))
df2 <- perWorkField %>% mutate(module = replace(module, module == "A", "ThisIsALongText"))
b <- ggplot() +
geom_point(data = df2, mapping = aes(x = percent, y = module)) +
xlim(c(0, 100))
pdf("test.pdf")
print(list(a, b))
dev.off()
Related Topics
Angle Between Two Vectors in R
Creating a Summary Statistical Table from a Data Frame
Extend Contigency Table with Proportions (Percentages)
Plot Polynomial Regression Curve in R
Fast Pairwise Simple Linear Regression Between Variables in a Data Frame
Deleting Reversed Duplicates with R
What Is the Algorithm Behind R Core's 'Split' Function
Remove Backslashes from Character String
Floating Point Less-Than-Equal Comparisons After Addition and Substraction
Remove Groups with Less Than Three Unique Observations
How to Access the Last Value in a Vector
How to Add Different Lines for Facets
R - Group by Variable and Then Assign a Unique Id
How to Create a Loop That Includes Both a Code Chunk and Text with Knitr in R