Reputation: 187
I'm making some bar charts in plotnine of counts on a single variable (based on survey responses).
I'm currently using this code:
(ggplot(df, aes ('age_cat3')) +
geom_bar(color='#F6F6F6', fill='#0C475B') +
theme_minimal() +
xlab("") +
ggtitle('Age distribution in total sample'))
And this produces:
I would like each bar to have a different color. I know I could do this by adding [fill= "age_cat3"], so:
(ggplot(df, aes ('age_cat3', fill='age_cat3')) +
geom_bar() +
theme_minimal() +
xlab("") +
ggtitle('Age distribution in total sample'))
And this produces:
There are two issues with this solution:
There in now a legend, which I can't seem to remove with "scale_color_discrete(guide=False)"
I can't seem to control the color or color theme.
Any advice on removing the legend would be great, but ideally, some way of changing the color of the bars without having to use "fill" would be great.
(also, sorry the images came out so big. I'm not quite sure how to attach small images)
reproducible data:
df = pd.DataFrame({'id': [1, 2, 3, 4, 5, 6, 7, 8 , 9, 10],
'age_cat3': [15-24, 15-24, 25-34, 35-44, 35-44, 35-44, 35-44, 45-59, 45-59, 60+]}
Upvotes: 1
Views: 4033
Reputation: 1021
Another option to remove the legend is:
(ggplot(...)
...
+ theme(legend_position = element_blank())
or place it in another position:
(ggplot(...)
...
+ theme(legend_position = 'top')
The scale_fill_manual
is the best and maybe the only way to include color values manually.
Upvotes: 1
Reputation: 2375
You can turn off the legend with geom/layer parameter show_legend=False
and you can use a manual scale to set the fill
colours.
(ggplot(...)
...
+ geom_bar(show_legend=False)
+ scale_fill_manual(values=['red', 'blue', 'green', 'cyan', 'violet', '#AABBCC'])
)
Upvotes: 2