简体   繁体   English

如何为列表中的每个 dataframe 创建 ggplot 并将它们导出到 R 中?

[英]How to create ggplot for each dataframe in a list and export them in R?

I have a list of dataframes of data separated with different names (eg, 57-block8, 58-block8, 59-block8, etc.).我有一个以不同名称分隔的数据数据帧列表(例如,57-block8、58-block8、59-block8 等)。 I wanna plot graphs for each of the split table using the same variables (ie, BIN_END_TIME on the x-axis and AVERAGE_PUPIL_SIZE_BIN on the y-axis), and have each plot named with the respective names of the dataframe. I wanna plot graphs for each of the split table using the same variables (ie, BIN_END_TIME on the x-axis and AVERAGE_PUPIL_SIZE_BIN on the y-axis), and have each plot named with the respective names of the dataframe. I also wanna draw some x-intercept vertical lines on each plot derived using the value of the last row of the BIN_END_TIME column.我还想在使用 BIN_END_TIME 列的最后一行的值导出的每个 plot 上绘制一些 x 截距垂直线。 Many thanks!非常感谢!

Setting up some data to provide as an example:设置一些数据以提供作为示例:

iris2 <- iris[,-5]
iris_list <- split(iris2, iris$Species)
str(iris_list)
List of 3
 $ setosa    :'data.frame': 50 obs. of  4 variables:
  ..$ Sepal.Length: num [1:50] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
  ..$ Sepal.Width : num [1:50] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
  ..$ Petal.Length: num [1:50] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
  ..$ Petal.Width : num [1:50] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ versicolor:'data.frame': 50 obs. of  4 variables:
  ..$ Sepal.Length: num [1:50] 7 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 ...
  ..$ Sepal.Width : num [1:50] 3.2 3.2 3.1 2.3 2.8 2.8 3.3 2.4 2.9 2.7 ...
  ..$ Petal.Length: num [1:50] 4.7 4.5 4.9 4 4.6 4.5 4.7 3.3 4.6 3.9 ...
  ..$ Petal.Width : num [1:50] 1.4 1.5 1.5 1.3 1.5 1.3 1.6 1 1.3 1.4 ...
 $ virginica :'data.frame': 50 obs. of  4 variables:
  ..$ Sepal.Length: num [1:50] 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 ...
  ..$ Sepal.Width : num [1:50] 3.3 2.7 3 2.9 3 3 2.5 2.9 2.5 3.6 ...
  ..$ Petal.Length: num [1:50] 6 5.1 5.9 5.6 5.8 6.6 4.5 6.3 5.8 6.1 ...
  ..$ Petal.Width : num [1:50] 2.5 1.9 2.1 1.8 2.2 2.1 1.7 1.8 1.8 2.5 ...

We have a list of 3 similar data frames.我们有一个包含 3 个相似数据框的列表。 The bind_rows() function from dplyr easily combines them together, and giving a name to the .id argument includes the name given to each data frame as an additional variable:来自dplyrbind_rows() function 很容易将它们组合在一起,并且为.id参数指定名称包括为每个数据帧指定的名称作为附加变量:

iris_keys <- dplyr::bind_rows(iris_list, .id = "keys")

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

You can now use that variable as a name for the facetting for your chart:您现在可以使用该变量作为图表分面的名称:

library(ggplot2)
ggplot(iris_keys, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point() +
  facet_wrap(vars(keys))

在此处输入图像描述

EDIT: If you want to create a list of plots, here's an example on achieving this:编辑:如果你想创建一个地块列表,这里有一个实现这个的例子:

library(purrr)

myplots <- map(iris_list, ~ ggplot(.x, aes(x = Sepal.Length, y = Petal.Length)) + 
      geom_point())

And the following line will save the plots to disk:以下行会将绘图保存到磁盘:

iwalk(myplots, ~ ggsave(glue::glue("{.y}.png"), .x, device = "png"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建一个for循环以删除重复项,并从R中数据框的不同列将其导出为csv - Create a for Loop to delete duplicates and to export them as csv from different columns of a dataframe in R 如何使用列表中的值使用R中的每个值在数据框中动态创建列? - How do I use values in a list to dynamically create columns in a dataframe using each value in R? R:如何将列表附加到列表,然后将每个列表分配给数据框中的一行? - R: How to append a list to a list, and then assign each list to a row in a dataframe? 在 R 中,如何创建新的 dataframe 以及其他 dataframe 中每个字符串的出现次数,如产品列表和已售出的篮子中的产品 - In R, how create new dataframe with number of occurrences of each strings on other dataframe, like a product list and products in basket sold 为 ggplot R 的多线图创建数据框 - create a dataframe for multiple line plot for ggplot R 如何拆分数据框,在每个列表数据框中创建新变量并取消拆分? - How to split dataframe, create new variable in each list dataframe, and unsplit? 列表中列表中的数据帧 - 如何访问 R 中的每个数据帧 - Dataframes within list within list - how to access each dataframe in R 结合来自 2 个不同数据框列表和 1 个数据框的数据,使用 GGplot R 创建图表列表 - Combine data from 2 different lists of dataframes and 1 dataframe to create a list of charts using GGplot R R上的Leaflet:如何为数据框中的每个因子级别创建图层和颜色 - Leaflet on R: How to create layers and colors for each factor level in dataframe 如何通过从 R 中的每个组中采样 1 个案例(行)来创建 dataframe - How to create a dataframe by sampling 1 case (row) from each group in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM