在ggplot2中使用for循环保存图形 [英] Saving graphs with for loop in ggplot2

查看:330
本文介绍了在ggplot2中使用for循环保存图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点迷恋ggplot2中的for loop.

我试图通过ggplot2中的for循环将Speciescateg名称添加到每个绘图标题以及文件名.不知何故,该循环似乎仅以一种物种"名称作为标题.

I am trying to adding Species and categ names to each plot title as well as the file name through the for loop in ggplot2. Somehow, the loop seems to taking only one Species name to title.

library(dplyr)
data_iris <- iris%>%
  mutate(categ=ifelse(Petal.Width<0.4,"A",ifelse(Petal.Width>=0.4&Petal.Width<=1.0, "B","C")))

> head(data_iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species categ
1          5.1         3.5          1.4         0.2  setosa     A
2          4.9         3.0          1.4         0.2  setosa     A
3          4.7         3.2          1.3         0.2  setosa     A
4          4.6         3.1          1.5         0.2  setosa     A
5          5.0         3.6          1.4         0.2  setosa     A
6          5.4         3.9          1.7         0.4  setosa     B

情节部分

for (i in unique(data_iris$Species)) {

for (j in unique(data_iris$categ)) {

  p = ggplot(data_iris[data_iris$categ==j,], aes(x=Sepal.Length, y=Sepal.Width)) +
    geom_point(size=3, aes(colour=categ))+

    labs(title=paste( i,j, "species_categ",sep="_")) #this part is not working!!!

  plot_list[[j]] = p
}
}  

# Save plots to tiff. Makes a separate file for each plot.


library(ggplot2)

for (i in unique(data_iris$Species)) {

for (j in unique(data_iris$categ)) {
  file_name = paste(i,j, "iris_plot_", ".tiff", sep="_")
  tiff(file_name)
  print(plot_list[[j]])
  dev.off()
}
}

ant输出是这样的(我没有添加所有图和名称.但是您会在工作目录中看到它们)

ant the output is like this (I didn't add all plots and names. But you will see them in working directory)

因此,正如我们所看到的,问题出在这里,我无法为每个图获得正确的Species名称.我听不懂吗?为什么会这样?

So, as we can see the problem is here I can't get the correct Species name for each plot. I could not get it? Why this is happening ?

推荐答案

尝试一下. 您的索引错误.首先,我可能会以不同的方式存储图-可能在列表列表中.

Try this. Your indexing is wrong. I would probably store the plots differently in the first place - maybe in a list of lists.

ind <- 1 # initialise the index for storing

for (i in unique(data_iris$Species)) {
  for (j in unique(data_iris$categ)) {

    p <- ggplot(data_iris[data_iris$categ==j,], aes(x=Sepal.Length, y=Sepal.Width)) +
      geom_point(size=3, aes(colour=categ))+

      labs(title=paste( i,j, "species_categ",sep="_")) 

    plot_list[[ind]] <- p  # stor the plot
    ind <- ind + 1         # increment   
  }
}  

ind <- 1
for (i in unique(data_iris$Species)) {
  for (j in unique(data_iris$categ)) {

    file_name = paste(i,j, "iris_plot_", ".tiff", sep="_")
    tiff(file_name)
    print(plot_list[[ind]]) # use the same index to retrieve the plot
    ind <- ind + 1
    dev.off()
  }
}

setosa_A_iris_plot__

setosa_A_iris_plot__

setosa_B_iris_plot__

setosa_B_iris_plot__

这篇关于在ggplot2中使用for循环保存图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆