循环遍历数据框列表以在R中创建图形 [英] Looping thru a list of dataframes to create a graph in R

查看:107
本文介绍了循环遍历数据框列表以在R中创建图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用xyplot创建了一个晶格散点图,将其分为不同的类别.现在,我尝试为散点图中的每个类别创建一个hexplot.我可以对变量进行硬编码,但我希望循环执行,因为我会多次进行此操作,这将产生新的类别.

I created a lattice scatterplot using xyplot that is grouped into separate categories. Now I am trying to create a single hexplot for each of the categories from the scatterplot. I can hard code the variables but I would prefer to do it in a loop since I would be doing this multiple times which will have new categories.

我从一个看起来像这样的表开始

I started with a table that looks like this

 Name     Category     Value1      Value2
sample1    cat1     10     1.5
sample2    cat2     10     1.5
sample3    cat3     10     1.5
sample4    cat1     10     1.5
sample5    cat1     10     1.5
sample6    cat2     10     1.5
sample7    cat3     10     1.5

我能够使用创建一个数据框列表

I was able to create a list of dataframes using

testing <- split(Mydata, Mydata$Category)

然后我可以创建一个图

testing2 <- as.data.frame(testing[["cat1"]]) #I keep on needing to change this for each Category that I have
ggplot(testing2, aes(x = testing2[,3], y = testing2[,4])) +
  geom_hex(bins = 30)

testing2看起来像这样

testing2 looks like this

 Name     Category     Value1      Value2
sample1    cat1     10     1.5
sample4    cat1     10     1.5
sample5    cat1     10     1.5

我尝试了

for(i in testing){
  testing3 <- i
  xtra <- ggplot(testing3, aes(x = testing3[,3], y = testing3[,4])) + geom_hex(bins = 30)
  xtra
}

最后,xtra是列表中的最后一个数据帧.

This ends up with xtra being the last dataframe on the list.

有人可以帮我吗?我希望能够创建图而不必每次都更改$ Category,因为每次我要执行的分类都超过50个.

Can someone help me with this? I would want to be able to create plots without having to change $Category each time as I have >50 categories for everytime that I want to do this.

-编辑1 根据建议,我创建了一个函数;

--edit1 As per suggestion, I created a function;

myFirstFun <- function(column)
{
  testing2 <- as.data.frame(testing[[column]])
  column <- enquo(column)
  ggplot(testing2, aes_string(x ="Value1", y = "Value2", group = column)) +
    geom_hex(bins = 30)
}

还有这个

myFirstFun("cat1")

产生这个;

 Name     Category     Value1      Value2
sample1    cat1     10     1.5
sample4    cat1     10     1.5
sample5    cat1     10     1.5

但是当我尝试使用for循环时;

but when I try to use a for loop;

for(i in categorynames){###categorynames is a vector that has all my categorynames
  myFirstFun(i)
}

它只会产生列表中的最后一个图形. 如何生成n个图形(n =我的类别数)? 没有我手动做

it will only produce the last graph on the list. How would I do this to produce n number of graphs (n = number of my categories)? Without me manually doing

myFirstFun("cat1")
myFirstFun("cat2")
myFirstFun("cat3")
...

推荐答案

您可以构建一个函数,在其中使用dplyr::filter选择所需的Category然后进行绘制.

You can built a function in which you use dplyr::filter to select the desired Category then do the plotting.

要遍历每个Category,请使用purrr::map并将所有结果存储在列表中.在这里,您可以打印您选择的图或将它们全部合并成一页,也可以多页

To loop through every Category, use purrr::map and store all results in a list. From there you can either print the plot of your choice or merge them all together in 1 page or multiple pages

library(tidyverse)

df <- read.table(text = "Name     Category     Value1      Value2
sample1    cat1     11     2.5
sample2    cat2     13     1.5
sample3    cat3     12     3.5
sample4    cat1     15     6.5
sample5    cat1     17     4.5
sample6    cat2     14     7.5
sample7    cat3     16     1.5",
                 header = TRUE, stringsAsFactors = FALSE)

cat_chart1 <- function(data, category){

  df <- data %>% 
    filter(Category == category)

  plot1 <- ggplot(df, aes(x = Value1, y = Value2)) + 
    geom_hex(bins = 30)

  return(plot1)
}

# loop through all Categories
plot_list <- map(unique(df$Category), ~ cat_chart1(df, .x)) 
plot_list[[1]]                 

# combine all plots
library(cowplot)
plot_grid(plotlist = plot_list, ncol = 2)

reprex软件包(v0.2.1.9000)创建于2019-04-04

Created on 2019-04-04 by the reprex package (v0.2.1.9000)

这篇关于循环遍历数据框列表以在R中创建图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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