我真的很喜欢使用purrr自动执行我的探索图,我通过以下博客文章了解了如何做:https://www.r-bloggers.com/2018/08/automating-exploratory-plots-with-ggplot2-and- rr /
但是,此博客文章中的代码存在一个问题,就是您必须在函数中指定数据集。我想在函数中归纳数据集,以便我可以根据需要更改数据集。
我已经尝试了所有我能想到的方法,但是确实被卡住了。有什么建议?
###Sample dataset
dataSet <- data.frame(id = c(1,1,2,2),
col1 = c(3.5,4.5,6.5,7.6),
col2 = c(1.2,2.3,4.5,6.7),
col3 = c(9.8,7.6,5.4,5.6),
timepoint = c(1,2,1,2))
###Sample function
plot_func <- function(x,y,d) {
ggplot(d, aes(x = .data[[x]], y = .data[[y]])) +
geom_line(aes(x = .data[[x]], y = .data[[y]], group = id), size = 1, alpha = 0.3)
}
###Specify variables of interest
exposures <- names(dataSet)[2:4]
timepoint <- names(dataSet)[5]
##Name variables of interest
exposures <- purrr::set_names(exposures)
timepoint <- purrr::set_names(timepoint)
###This doesn't work because need to specify the dataset, but no matter what I try I can't figure out where to put the dataset in the formula
plots = map(exposures,
~map(timepoint, plot_func, y = .x) )
如果仅循环exposures
一个时间点,则应执行以下操作:
plots = map(exposures, ~ plot_func(x = timepoint, y = .x, d = dataSet))
您可以只使用lamda表示法来填写要指定的变量。在这种情况下.x
是您的输入。map
exposures
请参见下面的代码:
library(ggplot2)
library(purrr)
###Sample dataset
dataSet <- data.frame(id = c(1,1,2,2),
col1 = c(3.5,4.5,6.5,7.6),
col2 = c(1.2,2.3,4.5,6.7),
col3 = c(9.8,7.6,5.4,5.6),
timepoint = c(1,2,1,2),
timepoint2 = c(3,4,3,4))
### Sample function
plot_func <- function(x,y,d) {
ggplot(d, aes(x = .data[[x]], y = .data[[y]])) +
geom_line(aes(x = .data[[x]], y = .data[[y]], group = id), size = 1, alpha = 0.3)
}
### Specify variables of interest
exposures <- names(dataSet)[2:4]
timepoint <- names(dataSet)[5]
timepoint2 <- names(dataSet)[5:6]
##Name variables of interest
exposures <- purrr::set_names(exposures)
timepoint <- purrr::set_names(timepoint)
timepoint2 <- purrr::set_names(timepoint2)
### This works
plots <- map(exposures, ~ plot_func(x = timepoint, y = .x, d = dataSet))
plots
#> $col1
#> $col2
#> $col3
如果要循环exposures
和时间点变量(我添加timepoint2
为另一个时间点列),则可以使用嵌套map
调用:
plots2 <- map(exposures, ~ map(timepoint2, function(z) plot_func(x = z, y = .x, d = dataSet)))
plots2
#> $col1
#> $col1$timepoint
#> $col1$timepoint2
#> $col2
#> $col2$timepoint
#> $col2$timepoint2
#> $col3
#> $col3$timepoint
#> $col3$timepoint2
由reprex软件包(v0.3.0)创建于2021-01-14
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句