❝本节来介绍在 R中如何使用ggplot2结合for循环绘图并保存,下面通过一个案例来看具体操作 ❞
library(tidyverse)
library(data.table)
library(gridExtra)
library(patchwork)
file_name <- "loop_data.tsv"
dat <- fread(file_name, sep="\t")
cities = unique(dat$city)
city_plots = list()
for(city_ in cities) {
city_plots[[city_]] <- ggplot(dat %>% filter(city == city_),aes(x=zone, y=`multistorey buildings`)) +
geom_bar(stat="identity",width=0.5) +
theme(axis.text.x = element_text(angle=0)) +
ggtitle(city_) +
labs(y=NULL,x=NULL)+
theme(plot.title=element_text(size=15,face="bold",hjust=0.5),
axis.title.x=element_text(size=10,face="bold"),
axis.title.y=element_text(size=10,face="bold"))
print(city_plots[[city_]])
ggsave(city_plots[[city_]],file=paste0("plot_",city_,".pdf"), width =3.04, height =3.10, units = "in", dpi=300)
}
上面我们将每一张图都单独输出了,下面来介绍如何将其全部组合起来,分别介绍两种R包的方法gridExtra&patchwork
grid.arrange(grobs=city_plots,ncol=3)
patchwork::wrap_plots(city_plots,nrow=2)