【问题标题】:Looping thru a list of dataframes to create a graph in R循环遍历数据框列表以在 R 中创建图形
【发布时间】:2019-08-26 10:22:04
【问题描述】:

我使用 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)

testing2 看起来像这样

 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 个类别。

--edit1 根据建议,我创建了一个函数;

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")
...

【问题讨论】:

  • 请使用dput(my_data)显示一些可重复的数据
  • 很难用给定的信息来判断,但如果这是关于 aes() 映射中的变量的问题,这可能是 R-FAQ 重复:stackoverflow.com/questions/22309285/…
  • @Gregor 值得注意的是,aes_stringaes_ 都可以工作,但正在/已经被弃用,取而代之的是 tidyeval
  • @Gregor 我现在将代码变成了函数,但我的问题仍然存在,因为我无法循环遍历列表来创建多个不同的图。 myFirstFun &lt;- function(column) { testing2 &lt;- as.data.frame(testing[[column]]) column &lt;- enquo(column) ggplot(testing2, aes_string(x ="Value1", y = "Value2", group = column)) + geom_hex(bins = 30) } 我可以创建一个绘图,但是当我尝试使用 for(i in categorynames){ myFirstFun(i) } 进行循环时,我仍然只能绘制列表中的最后一个类别。 categorynames 是一个包含我所有类别名称的向量

标签: r loops ggplot2 plot


【解决方案1】:

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

要遍历每个Category,请使用purrr::map 并将所有结果存储在一个列表中。从那里您可以打印您选择的图或将它们全部合并到一页或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 package (v0.2.1.9000) 于 2019 年 4 月 4 日创建

【讨论】:

  • 非常感谢。这解决了我的问题。干杯!
猜你喜欢
  • 2020-04-06
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 2021-02-08
  • 2019-06-21
  • 1970-01-01
  • 2018-09-28
  • 1970-01-01
相关资源
最近更新 更多