Reputation: 13
A percentage bar was made by ggplot2
, fill=cyl
created the default color (red/green/blue) for the bar. How can I change the color? Any ideas?
library(dplyr)
library(ggplot2)
mtcars %>%
count(cyl = factor(cyl)) %>%
mutate(pct = prop.table(n)) %>%
ggplot(aes(x = cyl, y = pct, fill=cyl, label = scales::percent(pct))) +
geom_col(position = 'dodge') +
geom_text(position = position_dodge(width = .9), # move to center of bars
vjust = -0.5, # nudge above top of bar
size = 3) +
scale_y_continuous(labels = scales::percent)
Upvotes: 1
Views: 1025
Reputation: 6483
You can use ggplot2
s scale_fill_manual
function:
library(dplyr)
library(ggplot2)
mtcars %>%
count(cyl = factor(cyl)) %>%
mutate(pct = prop.table(n)) %>%
ggplot(aes(x = cyl, y = pct, fill=cyl, label = scales::percent(pct))) +
geom_col(position = 'dodge') +
geom_text(position = position_dodge(width = .9), # move to center of bars
vjust = -0.5, # nudge above top of bar
size = 3) +
scale_y_continuous(labels = scales::percent) +
scale_fill_manual(values = c("#555555", "#554325", "#122355"))
We can use hex values for colors or names, i.e. something like
scale_fill_manual(values = c("orange", "black", "red"))
is also possible
Upvotes: 1