Home > Software engineering >  I'm only getting one color don't know why despite changing color name
I'm only getting one color don't know why despite changing color name

Time:11-08

I'm trying to change the color but it's stuck at one color. It's didn't happen before.

ggplot(data=diamonds, aes(x=color, fill="cyan")) geom_bar()

Anyone know why it's happening?

Thank you!

CodePudding user response:

that's because you choosing one specific color to fill everything ("cyan"). Try using a categorical value instead, like this:

ggplot(data=diamonds, aes(x=color, fill=cut)) 
geom_bar()

CodePudding user response:

There a fundamental difference between the structure geom_*(aes(), fill = ...) and geom_*(aes(fill = ...)). In the second case, fill will change according to "categories" specified in ....

See the example below:

library(ggplot2)

ggplot(data = iris)  
  geom_bar(aes(x = Species, fill = "any text"))  
  scale_fill_manual(values = c("any text" = "cyan"))


ggplot(data = iris)  
  geom_bar(aes(x = Species), fill = "cyan")

Created on 2022-11-08 with reprex v2.0.2

For more realistic use, please read the example below:

# A more realistic example:
ggplot(data = iris)  
  geom_point(aes(x = Sepal.Width, y = Petal.Length, color = Species))  
  scale_color_manual(values = c(
    "setosa" = "green", "versicolor" = "cyan", "virginica" = "red"
  ))
  • Related