Boxplot Error "Must request at least one color from a hue palette"

The problem here is that your fill is mapped to a variable that is entirely populated with NAs (see the output from when you run str(Run100.mw)). If you keep Gender as a character, or convert it to a factor, the code runs fine.

Run100.mw <- data.frame(
  YEAR = rep(c(2018, 2019), 10),
  Gender = rep(c("Male", "Male", "Female", "Female"), 5),
  MARK = rnorm(20, 5, 1), stringsAsFactors = FALSE
)

str(Run100.mw)
#> 'data.frame':    20 obs. of  3 variables:
#>  $ YEAR  : num  2018 2019 2018 2019 2018 ...
#>  $ Gender: chr  "Male" "Male" "Female" "Female" ...
#>  $ MARK  : num  4.96 3.96 5.74 5.98 5.23 ...

library(ggplot2)
ggplot(Run100.mw, aes(x = as.factor(YEAR), y = MARK, fill = Gender)) + 
  geom_boxplot()


Run100.mw$Gender <- as.factor(Run100.mw$Gender)
str(Run100.mw)
#> 'data.frame':    20 obs. of  3 variables:
#>  $ YEAR  : num  2018 2019 2018 2019 2018 ...
#>  $ Gender: Factor w/ 2 levels "Female","Male": 2 2 1 1 2 2 1 1 2 2 ...
#>  $ MARK  : num  4.96 3.96 5.74 5.98 5.23 ...

ggplot(Run100.mw, aes(x = as.factor(YEAR), y = MARK, fill = Gender)) + 
  geom_boxplot()

Created on 2019-06-25 by the reprex package (v0.3.0)

1 Like