Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set scale_fill_brewer and scale_fill_discrete at the same ggplot bar chart?

Tags:

r

ggplot2

I´m trying to create a bar chart, but I´m getting an error like:

Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.

I identified the problem as trying to use two scale_fill functions. If I remove one of them (scale_fill_brewer or scale_fill_discrete), it works.

ggplot(df_postgres, aes(x=as.factor(language), y=total/1000, fill=type)) + 
geom_bar(stat='identity', color = 'black')+
geom_text(mapping=aes(label=ifelse(type=='A', (paste(as.character(floor(total/1000)),'K') ), '')), vjust=-0.7,color='black')+
scale_x_discrete(name="Language")+
scale_y_continuous(name="Repos (thousands)")+
scale_fill_brewer(palette='Pastel1')+
scale_fill_discrete(name="Proportion",labels=c("Language","Others"))

My question is: how can I set the discrete options AND the nice palette option? I´m a newbie in R, but can´t find a response to this specific error.

like image 474
atorres Avatar asked Dec 28 '16 15:12

atorres


1 Answers

The ... argument to scale_fill_brewer will take arguments that will be passed to scale_fill_discrete.

So move your name and labels arguments to scale_fill_brewer, delete your scale_fill_discrete and everything should work.

like image 161
Richard Telford Avatar answered Oct 02 '22 15:10

Richard Telford