0

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

蜂群情节

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)
}    

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

(1 - h) * qs[i] 中的错误:二元运算符的非数字参数

我已经使用 cut() 函数将部分代码调整为:

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

我收到以下错误:

错误:美学必须是长度 1 或与数据 (22) 相同:填充

我相信上述错误的发生是由于数据中应用的过滤器(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 循环中为它们分配自定义颜色?

4

1 回答 1

0

if I understand your problem correctly, you could "preprocess" the data before building the plot by adding a new column with the ntile() function (number according to quantile), than convert this new colum to a factor and finally build the plot with mapping this new column to the color asthetic.

于 2020-10-31T14:21:17.920 回答