To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
This reproduced your error. Note that Gender has only NA values. I suggest you look at the data frame content just before you plot the data. If this hint does not solve your problem, please make a reprex as requested by andresrcs.
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)
Run100.mw$Gender <- as.factor(Run100.mw$Gender)
Run100.mw$Gender <- factor(Run100.mw$Gender, levels=c(1,2), labels=c("Male", "Female"))
str(Run100.mw)
#> 'data.frame': 20 obs. of 3 variables:
#> $ YEAR : num 2018 2019 2018 2019 2018 ...
#> $ Gender: Factor w/ 2 levels "Male","Female": NA NA NA NA NA NA NA NA NA NA ...
#> $ MARK : num 5.64 5.38 5.4 6.61 5.28 ...
library(ggplot2)
ggplot(Run100.mw, aes(x= as.factor(YEAR), y = MARK, fill=Gender)) + geom_boxplot()
#> Error: Must request at least one colour 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()