'Change bar colours in a grouped bar chart in ggplot, while also keeping the labels for the groups
Good evening everybody,
I have a problem with some descriptive figures I'm making in RStudio using ggplot2. For a project, I'm comparing a lot of categorical variables between vulnerable and non-vulnerable people (e.g. health status, fruit and vegetable consumption etc.). I've managed to create grouped bar charts using ggplot2 to show the percentages of both groups for each variable I'm interested in. Everything worked fine until I had to change the colours of the bars to the company colours instead of the standard colours RStudio picks. Now when I want to change the colours the labels for the legend (which show the scale of the variables) the labels are removed. It shows the correct colour but not the labels. Do you know how I can fix this? Thank you a lot in advance!
data%>%
group_by(Kwetsbaar) %>%
count(Gezondheidsstatus) %>%
mutate(pct = n / sum(n)) %>%
ggplot(aes(Kwetsbaar, pct, fill = factor(Gezondheidsstatus))) +
geom_col(position = 'dodge') +
labs(x = "Groepen", y = "Percentage", title = "Algemene gezondheid") +
scale_y_continuous(labels = scales::percent_format()) +
scale_x_discrete(labels = c("Kwetsbaar", "Niet kwetsbaar")) +
scale_fill_discrete(name="Legende",labels=c("Zeer slecht", "Slecht", "Redelijk","Goed","Gemakkelijk","Zeer goed","Niet van toepassing"))+
scale_fill_manual(values = c("green4", "green3","green2","green1","greenyellow"))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
