[英]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:来自dplyr
的bind_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.