J_p
J_p

Reputation: 475

Change colors to the 'fill' in aes?

I have this:

colr=c(a='black',b='red',c='brown')

Basically, i have used the fill with the categories from a column in the df in the aes. Thus it will show through the categories the plot. The problem is when i try to put the colr vector in the fill to change colors as it says it encounters the problem

Error: Aesthetics must be either length 1 or the same as the data (5): fill

Obviously the incorrect way of typing it makes it think that the colors refer to the brandscolumn while it should refer to the g_classes in the fill.

 ggplot(df,aes(brands,fill=g_classes))+geom_bar(stat='count',fill=colr)

So, how to pass the colors in the colr vector to the fill (g_classes) in aes?

Upvotes: 2

Views: 1319

Answers (1)

FredrikH-R
FredrikH-R

Reputation: 152

You have an option to use the scale_fill_manual command. It has to be the same length as you have categories, however. In this case it seems that you are attempting to specify three colours for five categories, and this is most likely why your code fails.

It is hard to reproduce your problem given the limited examples you give, but try omitting the fill argument in the geom_bar command, and changing the value for the stat argument to "identity"

Change your colour vector to

colr=c("black","red","brown")

and add additional ggplot line

scale_fill_manual(values=colr)+

Upvotes: 1

Related Questions