I would like to create a stacked bar chart where my axis.text takes it's color values from a variable in the data frame that also provides the bar's fill color. This is very important because the consumers of the final visuals will be viewing a series of these bar charts so I need to make sure the colors are consistent for each product type even though the Amount values (and thus the order) will vary. The below is the closest I can get.
# My data sample
df <- data.frame(x=1:4, Type = c("Metals", "Foodstuff", "Textiles", "Machinery"), myColour = c('blue', 'red', 'green', 'orange'), Amount = c(75, 50, 25, 5))
# Create factor to order by amount value
df$Type <- factor(df$Type, levels = df[order(df$Amount), "Type"])
# MAKE BAR
gg1 <- ggplot(df, aes(Type, Amount, fill = Type, color = myColour)) +
geom_bar(stat = 'identity', position = 'dodge', show.legend = FALSE, width = .85, colour = 'lightgrey', fill = df$myColour) +
#ggtitle("Exports Profile (%)") +
labs(x = NULL, y = NULL) +
scale_y_continuous(breaks = waiver(), limits = c(0,100)) +
theme(#plot.title = element_text(family= 'sans', color = 'black', size = 28),
#axis.title = element_text(family= 'sans', color = 'black', size = 24),
axis.text.y = element_text(colour = df$myColour, size = 18, face = 'bold'),
axis.ticks.y = element_blank(),
axis.text.x = element_text(colour = 'black', size = 16),
axis.ticks.x = element_line(colour = 'grey60'),
axis.ticks.length = unit(3, "mm"),
axis.line = element_line(NULL),
plot.background = element_rect(fill = NULL),
panel.background = element_rect(fill = 'white', colour = 'white'),
panel.grid.major.x = element_line(colour = 'grey60', linetype = 'dashed'),
panel.grid.major.y = element_line(colour = 'grey60', linetype = 'dashed'),
#panel.margin = unit(c(0,0,0,0), "mm"),
aspect.ratio = (600/450)) +
coord_flip()
gg1
