My goal is to assign custom colours to the quantiles (first 25% of the data, second 25%, et cetera) for multiple beeswarm plots within a for loop. I have added my code and an image of a beeswarm plot. The grey coloured dots (see plot) need to be divided in four approximately equal groups in order to assign the custom colours to the dots. I am trying to do this with cut() and 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)
}
When I run the above stated code, I receive the following error:
Error in (1 - h) * qs[i] : non-numeric argument to binary operator
I have adjusted part of the code with the cut() function into:
cut(quantile(df[[i]]), breaks = 4)
I receive the following error:
Error: Aesthetics must be either length 1 or the same as the data (22): fill
I believe the above stated error occurs due to the filter applied in the data (23 data points to 22).
Some code to work with:
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"
))
How could I obtain the quantiles of the data and assign custom colours to them within the for loop?
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.