gpt4 book ai didi

r - ggplots 存储在绘图列表中以在 for 循环中生成绘图时尊重变量值

转载 作者:行者123 更新时间:2023-12-04 09:38:50 26 4
gpt4 key购买 nike

我有一个精心设计的绘图程序,它生成带有附加散点层的箱形图,并将它们添加到绘图列表中。

如果它们是在 for 循环期间直接通过 print(current_plot_complete) 创建的,则该例程会生成正确的图。 .

但是,如果在 for 循环期间将它们添加到仅在最后打印的绘图列表中,则绘图是不正确的:最终索引用于生成所有绘图(而不是生成绘图时的当前索引) )。
这似乎是默认值 ggplot2行为,我正在寻找一种解决方案来在当前用例中规避它。

问题似乎在 y = eval(parse(text=(paste0(COL_i)))) 之内使用全局环境(以及最终的索引值)而不是循环执行时的当前值。

我尝试了各种方法来制作 eval()使用正确的变量值,例如local(…)或指定环境 - 但没有成功。

下面提供了一个非常简化的 MWE。

enter image description here

移动电源

原始例程比这个 MWE 复杂得多,例如 for循环不能被 apply 的成员轻易替换家庭。

# create some random data
data_temp <- data.frame(
"a" = sample(x = 1:100, size = 50),
"b" = rnorm(n = 50, mean = 45, sd = 1),
"c" = sample(x = 20:70, size = 50),
"d" = rnorm(n = 50, mean = 40, sd = 15),
"e" = rnorm(n = 50, mean = 50, sd = 10),
"f" = rnorm(n = 50, mean = 45, sd = 1),
"g" = sample(x = 20:70, size = 50)
)
COLs_current <- c("a", "b", "c", "d", "e") # define COLs of data to include in box plots
choice_COLs <- c("a", "d") # define COLs of data to add scatter to

plot_list <- list(NA)
plot_index <- 1

for (COL_i in choice_COLs) {

COL_i_index <- which(COL_i == COLs_current)

# Generate "basis boxplot" (to plot scatterplot on top)
boxplot_scores <- data_temp %>%
gather(COL, score, all_of(COLs_current)) %>%
ggplot(aes(x = COL, y = score)) +
geom_boxplot()

# Get relevant data of COL_i for scattering: data of 4th quartile
quartile_values <- quantile(data_temp[[COL_i]])
threshold <- quartile_values["75%"] # threshold = 3. quartile value
data_temp_filtered <- data_temp %>%
filter(data_temp[[COL_i]] > threshold) %>% # filter the data of the 4th quartile
dplyr::select(COLs_current)

# Create layer of scatter for 4th quartile of COL_i
scatter_COL_i <- geom_point(data=data_temp_filtered, mapping = aes(x = COL_i_index, y = eval(parse(text=(paste0(COL_i))))), color= "orange")

# add geom objects to create final plot for COL_i
current_plot_complete <- boxplot_scores + scatter_COL_i

print(current_plot_complete)

plot_list[[plot_index]] <- current_plot_complete
plot_index <- plot_index + 1
}

plot_list

最佳答案

我提出了这个解决方案,它不会告诉你为什么它不像你那样工作:

l <- lapply(choice_COLs, temporary_function)

temporary_function <- function(COL_i){
COL_i_index <- which(COL_i == COLs_current)

# Generate "basis boxplot" (to plot scatterplot on top)
boxplot_scores <- data_temp %>%
gather(COL, score, all_of(COLs_current)) %>%
ggplot(aes(x = COL, y = score)) +
geom_boxplot()

# Get relevant data of COL_i for scattering: data of 4th quartile
quartile_values <- quantile(data_temp[[COL_i]])
threshold <- quartile_values["75%"] # threshold = 3. quartile value
data_temp_filtered <- data_temp %>%
filter(data_temp[[COL_i]] > threshold) %>% # filter the data of the 4th quartile
dplyr::select(COLs_current)

# Create layer of scatter for 4th quartile of COL_i
scatter <- geom_point(data=data_temp_filtered,
mapping = aes(x = COL_i_index,
y = eval(parse(text=(paste0(COL_i))))),
color= "orange")

# add geom objects to create final plot for COL_i
current_plot_complete <- boxplot_scores + scatter

return(current_plot_complete)
}

当您使用 lapply你没有这样的问题。
它的灵感来自 this post

关于r - ggplots 存储在绘图列表中以在 for 循环中生成绘图时尊重变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62423707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com