stixmcvix
stixmcvix

Reputation: 333

Error in ggplot2 density plot: "Error: Must request at least one colour from a hue palette."

Having installed tidyverse, I can't get the following to work....

ggplot(iris, aes(x = Sepal.Length, fill = Species) + geom_density(alpha = .3)

The error reads: "Error: Must request at least one colour from a hue palette."

Any ideas?

Upvotes: 12

Views: 56962

Answers (5)

abhivij
abhivij

Reputation: 107

This error comes if the column given as 'fill' contain all NAs.

So effectively it does not request for any color.

That would explain the error

"Error: Must request at least one colour from a hue palette."

I tried the below code, with 'condition' containing only NAs, and obtained the same error

  ggplot(grouped_info) +
    geom_bar(aes(x = label, y = n, fill = condition), stat = "identity")

Upvotes: 0

andhherson
andhherson

Reputation: 37

I had the same problem because I was calling a column witch a forgot to fill.

Upvotes: 1

amann
amann

Reputation: 369

just for the record and for future readers, sometimes the sourcing of the code does not work

source("code_that_generates_data_for_ggplot.R")

and the data that is the GGPLOT input have one or more missing variables. This happens since some of the data is encoded in UTF-8.

A possible solution to this is using

source("code_that_generates_data_for_ggplot.R", encoding="UTF-8")

instead

Upvotes: 1

Carles Borredá
Carles Borredá

Reputation: 358

Just for the record and for future readers, I had this error related with the specific column (iris$Species in this example) containing only NAs.

This happened because some libraries did not work properly after sourcing my code, and therefore some tables didn't have the proper format.

Upvotes: 11

Sal-laS
Sal-laS

Reputation: 11649

I cannot get this error from your code. Your code works well, you just missed a close parentheses ):

ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
         geom_density(alpha = .3)

The result is:

enter image description here

Upvotes: 6

Related Questions