r - 在ggplot2中将facet_wraps分割为多个.png输出

标签 r plot ggplot2 facet-wrap

我正在尝试为数据中的每组生成多个方面图。我面临的问题是,在我的真实数据中,有时每组的构面超过 100 个,而 ggplot2 试图将所有构面放入一个图中。这不利于视觉效果。

基本上,我想要实现的是,当每页超过一定数量的构面窗口时(假设每页绘制 10 个窗口),我想继续在每组的下一个图表中绘制它们。

实现此目的的最小示例仅涵盖每组的绘制方面。

x <- rnorm(25,0,5)
y <- rnorm(25,5,10)

gr=rep(c(1,2),c(20,5))

set <- c(rep(1:20,1),1:5)

test_data <- data.frame(x,y,set,gr)

utils::View(test_data)

对于这个最小示例,gr 1 的设置数量为 20。但我想将其分开,也就是说每页 10 个面。

library(ggplot2)
library(gtable)
library(grid)
library(gridExtra)

plot_list=list()

for (i in unique(test_data$gr)){


plot_sets<- ggplot(data = test_data[test_data$gr==i,], aes(y = y, x=x,fill=factor(set))) +


geom_point(alpha = 0.8,size=8,shape=21,col="black",aes(group=set)) +


  facet_wrap(~set)+


  theme_bw()+
  theme(legend.position = "top",plot.title=element_text(face="bold",colour ="Blue",size=25),
        axis.title.x = element_text(face="bold",size=24), 
        axis.title.y = element_text(face="bold",size=24),
        #axis.text.x = element_blank(),
        axis.text.x = element_text(size = 22 , colour = "black",angle=0,vjust = 0.0, hjust = 0),
        axis.text.y = element_text(size = 22 , colour = "black"),
        strip.text = element_text(size=22, lineheight=1.0,face="bold"),
        strip.background = element_rect(fill="#FFFFCC", colour="black", size=0.1),
        legend.title = element_text(colour="black", size=20,face="bold"),
        legend.text = element_text(colour="black", size=26),
        panel.spacing = unit(0.2, "lines"))+
  labs(fill='set',col="set",x = "x",y="y",title=paste(i,j,"my_Sets",sep="_"))+
  guides(fill = guide_legend(override.aes = list(alpha=1,nrow=1,size=5)))  

  plot_list[[i]]=plot_sets

}

以.png格式保存图表

for (i in 1:length(unique(test_data$gr))) {

  file_name = paste(unique(test_data$gr), "my_sets.png", sep="_")

  png(paste("C:/test","/",file_name[i],sep=""), width =6400, height =4400, pointsize = 16, units = 'px', res = 200)

  grid.draw(plot_list[[i]])
  dev.off()
}

我发现了一些相关帖子,例如

Multiple graphs over multiple pages using ggplot

ggplot2: Plots over Multiple pages

但他们使用 .pdf 输出,这个示例是最相关的,但它适用于 .pdf 输出

带有 .pdf 输出版本的@agstudy 解决方案

tab <- data.frame(Date = rep(seq(as.Date('2013-03-01'),
                       by = 'day', length.out = ii), nn),
                   value = rep(runif(nn, 100, 200)))

tab$Station <- rep(1:nn, each = ii)

*library(gridExtra)
library(ggplot2)
pdf('test.pdf', width=21, height=27)
i = 1
plot = list() 
for (n in unique(tab$Station)){
  ### process data for plotting here ####
  plot[[i]] = ggplot(tab[tab$Station==n,], aes(x=Date,y = value)) +

    geom_line() +
    facet_wrap(~ Station, ncol = 5)

  if (i %% 20 == 0) { ## print 8 plots on a page
    print (do.call(grid.arrange,  plot))
    plot = list() # reset plot 
    i = 0 # reset index
  }
  i = i + 1
}

if (length(plot) != 0) { 
  print (do.call(grid.arrange,  plot))
}
dev.off()

最佳答案

这是一个使用 cut()ggsave() 的示例。

假设您有 80 个集合想要进行多面包裹。

library(tidyverse)

param_n <- 800

data <- data_frame(
  x = rnorm(param_n, 0, 5),
  y = rnorm(param_n, 5, 10),
  group = factor(sample(c(1, 0), param_n, TRUE)),
  set = sample(1:(param_n / 10), param_n, TRUE)
) 

这对于一个图来说是多图,因此您使用 cut() 将其拆分为十页。

data_cut <- 
  data %>%
  mutate(page = cut(set, 10))           # split into 10 groups

然后,您只需循环每个页面,filter() 取出该数据,并使用 ggsave() 保存一个 .png 文件。

for (current_page in levels(data_cut$page)) {

  fig <- 
    data_cut %>%
    filter(page == current_page) %>%
    ggplot(
      aes(y = y, x = x,
          colour = group,
          group = set)) +
    facet_wrap(~ set) +
    geom_point() +
    labs(title = paste("Sets", current_page)) +
    theme_bw() +
    theme(legend.position = "bottom")

  ggsave(
    paste0("figures_sets_", current_page, ".png"),
    plot = fig
  )

}

Example output

我简化了你的情节,使代码更加紧凑。

关于r - 在ggplot2中将facet_wraps分割为多个.png输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50301583/

相关文章:

r - 从保存为 r 中列表列的模型中提取模型信息

r - 3组列表中最佳和组合的算法

R 编码,我试图将数据帧中的变量从 1 到 13 正确排序,但它就像 201501、2015010、011,012,013、02...09

r - ggplot 中的自定义形状(geom_point)

r - "Unitless",定性或相对轴刻度ggplot2

r - 如何将 ggplot2 绘制的图保存为 SVG

r - 如何在 R data.table 中按行使用 ifelse?

r - NA == 1 检查返回 NA

r - 在 x 轴上绘制带有因子变量的折线图

R:具有小倍数/子图的绘图纵横比