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

Hello,

I am very new to R and therefore I need some advices.
I want to plot a grouped Boxplots on one axis.
Therefore I used this code:

Run100.mw$Gender <- as.factor(Run100.mw$Gender)
Run100.mw$Gender <- factor(Run100.mw$Gender, levels=c(1,2), labels=c("Male", "Female"))
library(ggplot2)
ggplot(Run100.mw, aes(x= as.factor(YEAR), y = MARK, fill=Gender)) + geom_boxplot()

Unfortunately it returns this error code and I am not able to identify my mistake:

Must request at least one colour from a hue palette.

Thank you in advance for your support,

Kind regards
CEFC

Hi, welcome!

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.

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

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

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.