在R中循环遍历数据框列表以创建图表

3
我使用xyplot创建了一个分组的格子散点图。现在我正在尝试为散点图中的每个类别创建单独的六边形散点图。我可以硬编码变量,但是我更愿意使用循环来做这件事,因为我将多次执行此操作,并且会有新的类别。
我从一个表格开始,它长这样:
 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

我能够使用以下方法创建数据帧列表:

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)

测试2的外观如下所示

 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成为列表中的最后一个数据框。
有人能帮忙吗?我想能够创建图表,而不必每次都更改$Category,因为我有50个以上的类别需要这样做。
-- 编辑1 根据建议,我创建了一个函数;
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循环时;
for(i in categorynames){###categorynames is a vector that has all my categorynames
  myFirstFun(i)
}

它只会生成列表中的最后一个图表。 我如何才能生成n个图表(其中n =我的类别数)?而不需要我手动执行


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

1
请使用 dput(my_data) 显示一些可重现的数据。 - Roman
很难根据给定的信息判断,但如果这是关于aes()映射中变量的问题,那么这可能是R-FAQ的重复问题:https://dev59.com/mWEh5IYBdhLWcg3whT3T - Gregor Thomas
2
@Gregor 值得注意的是,aes_stringaes_都可以使用,但已经被弃用,建议使用tidyeval。 - camille
可能是如何在ggplot中使用变量指定列名的重复问题。 - camille
@Gregor 我已经将代码转换为函数,但我的问题仍然存在,因为我无法循环遍历列表以创建多个不同的图形。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) } 我可以创建一个图形,但是当我尝试使用for(i in categorynames){ myFirstFun(i) }进行循环时,我仍然只能绘制列表中最后一个类别。categorynames是一个包含所有类别名称的向量。 - Juan Paolo Sicat
显示剩余3条评论
1个回答

2
您可以编写一个函数,其中使用 dplyr::filter 选择所需的 Category,然后进行绘图。
要循环遍历每个 Category,请使用 purrr::map 并将所有结果存储在列表中。从那里,您可以打印所选的图表,或将它们全部合并到1页或多个页面中。
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 package (v0.2.1.9000) 于2019年04月04日创建


1
非常感谢。这解决了我的问题。干杯! - Juan Paolo Sicat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接