在 for 循环内的蜂群图中为分位数分配颜色

Assign colours to quantiles in a beeswarm plot within a for loop

我的目标是为 for 循环中的多个蜂群图的分位数(数据的前 25%,第二个 25%,等等)分配自定义颜色。我已经添加了我的代码和蜂群图的图像。灰色点(见图表)需要分成四个大致相等的组,以便将自定义颜色分配给点。我正在尝试使用 cut() 和 quantile() 来做到这一点。

Beeswarm plot

colNames <- names(df)[15:18]
for(i in colNames){
  plot <- ggplot(data = df %>% filter(player != "F. Gago"), aes_string(x = factor(0), y = i), groupOnX = FALSE) +
    geom_quasirandom(shape = 21, fill = **cut(quantile(i))**, size = 12) +
    scale_fill_manual(values = c("Red", "Orange", "Yellow", "Green")) +
    labs(title = i) +
    theme(axis.title.y = element_blank(),
          axis.text.y = element_blank(),
          axis.ticks.y = element_blank(),
          axis.title.x = element_blank(),
          axis.text.x = element_text(size = 20, colour = "black"),
          axis.ticks.x = element_blank(),
          plot.title = element_text(size = 25, vjust = 3, colour = "black", face = "bold"),
          plot.caption = element_text(vjust = -6, face = "italic"),
          plot.margin = unit(c(1, 1, 1, 1), "cm"),
          panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank(),
          panel.background = element_blank(), 
          axis.line.x = element_line(colour = "black", size = 2)) +
    geom_point(data = df %>% filter(player == "F. Gago"), aes_string(y = i), shape = 21, colour = "white", fill = "#62150F", size = 14) +
    coord_flip() 
  print(plot)
  Sys.sleep(2)
}    

当我运行上述代码时,我收到以下错误:

Error in (1 - h) * qs[i] : non-numeric argument to binary operator

我已经将带有cut()函数的部分代码调整为:

cut(quantile(df[[i]]), breaks = 4)

我收到以下错误:

Error: Aesthetics must be either length 1 or the same as the data (22): fill

我认为上述错误是由于数据中应用的过滤器(23 个数据点到 22 个数据点)造成的。

一些可使用的代码:

structure(list(player = c("F. Vera", "G. Giménez", "L. Romero", 
"M. Pittón", "L. Vera", "E. Pérez"), team = c("Argentinos Juniors", 
"Chicago Fire", "Independiente", "Vélez Sarsfield", "Lanús", 
"River Plate"), position = c("DMF, RDMF", "DMF, LCB, LDMF", "DMF, LCMF, LDMF", 
"DMF, LDMF, RDMF", "RDMF, RCMF, LCMF", "DMF"), age = c(20, 28, 
26, 25, 23, 34), market_value = c(9e+06, 3e+06, 3e+06, 2e+06, 
2e+06, 2e+06), contract_expires = c("2021-06-30", "2021-12-31", 
"2023-06-30", "2023-06-30", "2021-06-30", "2023-06-30"), matches_played = c(14, 
19, 19, 11, 20, 18), minutes_played = c(973, 1595, 1718, 589, 
1571, 1625), birth_country = c("Argentina", "Argentina", "Argentina", 
"Argentina", "Argentina", "Argentina"), passport_country = c("Argentina", 
"Argentina", "Argentina", "Argentina", "Argentina", "Argentina"
), foot = c("right", "left", "right", "right", "right", "right"
), height = c(179, 180, 167, 181, 164, 178), weight = c(74, 78, 
70, 70, 60, 77), on_loan = c("no", "no", "no", "no", "no", "no"
), p_adj_interceptions = c(6.93, 8.33, 10.58, 6.75, 4.46, 10.29
), progressive_runs_per_90 = c(1.11, 2.09, 0.37, 0.92, 3.04, 
1.22), smt_passes_per_90 = c(0.18, 0.39, 0.31, 0.46, 2.52, 0.55
), progressive_passes_per_90 = c(6.29, 8.35, 7.86, 6.57, 10.71, 
11.91)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

如何获取数据的分位数并在 for 循环中为它们分配自定义颜色?

如果我正确理解你的问题,你可以在构建绘图之前“预处理”数据,方法是添加一个带有 ntile() 函数的新列(数字根据分位数),而不是将这个新列转换为一个因子,然后最后通过将这个新列映射到颜色美学来构建绘图。