如何在for循环中将ggplot2 aes_string()与cut()一起使用?

如何在for循环中将ggplot2 aes_string()与cut()一起使用?,r,ggplot2,R,Ggplot2,我试图循环一些连续变量来创建ggplot。这对aes_字符串很好,但我现在尝试了1000种变体,在调用中包括cut,以生成变量的bin。但它要么失败,要么循环不工作,并且在aes中始终使用相同的变量值 在我的实际数据中,我试图预先计算cut的中断,类似于cut_intervaln=6,因为每个变量都有不同的范围,但这也没有帮助 library(tidyverse) data(diamonds) diamonds <- head(diamonds, 200) # select some n

我试图循环一些连续变量来创建ggplot。这对aes_字符串很好,但我现在尝试了1000种变体,在调用中包括cut,以生成变量的bin。但它要么失败,要么循环不工作,并且在aes中始终使用相同的变量值

在我的实际数据中,我试图预先计算cut的中断,类似于cut_intervaln=6,因为每个变量都有不同的范围,但这也没有帮助

library(tidyverse)
data(diamonds)
diamonds <- head(diamonds, 200)

# select some numeric categories to loop over
categories <- names(diamonds)[c(1,5,6)]

# this works fine in a loop
plot_list <- list()
for (category in categories){
plot_list[[category]] <- ggplot(diamonds, aes(x = x, y = z)) +
    geom_point(data = diamonds[diamonds$color == "E", ], aes_string(fill = category), colour = "grey50", pch = 21) +
    geom_point(data = diamonds[diamonds$color != "E", ], aes_string(fill = category, colour = "price"), pch = 21)
}
plot_list

# together with cut(), it does not work anymore
cut_plot_list <- list()
for (category in categories){
  cut_plot_list[[category]] <- ggplot(diamonds, aes(x = x, y = z)) +
    geom_point(data = diamonds[diamonds$color == "E", ], aes_string(fill = cut(category, breaks = c(-Inf, 1, 10, 20, Inf)), colour = "grey50", pch = 21) +
    geom_point(data = diamonds[diamonds$color != "E", ], aes_string(fill = cut(category, breaks = c(-Inf, 1, 10, 20, Inf))), colour = "price"), pch = 21)
}
# fails: 'x' must be numeric

# this gives identical plots without fill
cut_plot_list <- list()
for (category in categories){
  cut_plot_list[[category]] <- ggplot(diamonds, aes(x = x, y = z)) +
    geom_point(data = diamonds[diamonds$color == "E", ], aes(fill = cut(get(category), breaks = c(-Inf, 1, 10, 20, Inf)), colour = "grey50", pch = 21) +
    geom_point(data = diamonds[diamonds$color != "E", ], aes(fill = cut(get(category), breaks = c(-Inf, 1, 10, 20, Inf)), colour = price), pch = 21)
}
cut_plot_list

我们可以使用非标准评估:

library(ggplot2)

apply_fun <- function(category) {
   ggplot(diamonds, aes(x = x, y = z)) +
       geom_point(data = diamonds[diamonds$color == "E", ], 
           aes(fill = cut(!!sym(category), breaks = c(-Inf, 1, 10, 20, Inf))), 
           colour = "grey50", pch = 21) +
       geom_point(data = diamonds[diamonds$color != "E", ], 
           aes(fill = cut(!!sym(category), breaks = c(-Inf, 1, 10, 20, Inf)) , 
           colour = price), pch = 21)
}
要将数据切割成n个间隔,我们可以

apply_fun <- function(category, n) {

  breaks = seq(min(diamonds[[category]]), max(diamonds[[category]]), length.out = n)
  ggplot(diamonds, aes(x = x, y = z)) +
    geom_point(data = diamonds[diamonds$color == "E", ], 
               aes(fill = cut(!!sym(category), breaks = breaks)), 
               colour = "grey50", pch = 21) +
    geom_point(data = diamonds[diamonds$color != "E", ], 
               aes(fill = cut(!!sym(category), breaks = breaks) , 
               colour = price), pch = 21)
}
将函数应用于

plot_list <- lapply(categories, apply_fun, n = 6)

我们可以使用非标准评估:

library(ggplot2)

apply_fun <- function(category) {
   ggplot(diamonds, aes(x = x, y = z)) +
       geom_point(data = diamonds[diamonds$color == "E", ], 
           aes(fill = cut(!!sym(category), breaks = c(-Inf, 1, 10, 20, Inf))), 
           colour = "grey50", pch = 21) +
       geom_point(data = diamonds[diamonds$color != "E", ], 
           aes(fill = cut(!!sym(category), breaks = c(-Inf, 1, 10, 20, Inf)) , 
           colour = price), pch = 21)
}
要将数据切割成n个间隔,我们可以

apply_fun <- function(category, n) {

  breaks = seq(min(diamonds[[category]]), max(diamonds[[category]]), length.out = n)
  ggplot(diamonds, aes(x = x, y = z)) +
    geom_point(data = diamonds[diamonds$color == "E", ], 
               aes(fill = cut(!!sym(category), breaks = breaks)), 
               colour = "grey50", pch = 21) +
    geom_point(data = diamonds[diamonds$color != "E", ], 
               aes(fill = cut(!!sym(category), breaks = breaks) , 
               colour = price), pch = 21)
}
将函数应用于

plot_list <- lapply(categories, apply_fun, n = 6)

太好了,谢谢你!是否有一种方法可以同时提供一个对象作为cutbreaks=参数的输入?我尝试了填充=切割间隔!!symcategory,n=6在调用中为每个类别设置配件箱,但这将给我总共12个箱子,每个箱子6个用于颜色==E和颜色!=E.我会在ggplot调用之前定义中断,但由于它们不是中断,我不能使用!!sym@crazysantaclaus我已经更新了答案。这就是你想要的吗?太完美了!你能给我解释一下,为什么变量不用就可以工作!!sym,但类别没有?我还将稍微编辑您的代码,因为我忘了在这里使用pch,它实际上可以显示fill@crazysantaclaussym仅用于列,而不用于诸如中断之类的值。这非常有效,谢谢!是否有一种方法可以同时提供一个对象作为cutbreaks=参数的输入?我尝试了填充=切割间隔!!symcategory,n=6在调用中为每个类别设置配件箱,但这将给我总共12个箱子,每个箱子6个用于颜色==E和颜色!=E.我会在ggplot调用之前定义中断,但由于它们不是中断,我不能使用!!sym@crazysantaclaus我已经更新了答案。这就是你想要的吗?太完美了!你能给我解释一下,为什么变量不用就可以工作!!sym,但类别没有?我还将稍微编辑您的代码,因为我忘了在这里使用pch,它实际上可以显示fill@crazysantaclaussym仅用于列,而不用于断开之类的值。