I have a stacked bar plot, labelled with geom_text
. To increase the visibility of the labels, I want to set the label color to either white or black depending o
Two ways to solve this issue by playing with geom_bar
color or size:
Data$LabelColor <- as.factor(Data$LabelColor)
p <- ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency, colour = LabelColor)) +
geom_bar(stat = "identity", color = "black") +
geom_text(size = 3, position = position_stack(vjust = 0.5)) +
scale_fill_manual(values = colorsPerCat) +
scale_colour_manual(values = levels(Data$LabelColor)) +
guides(colour = FALSE)
Data$LabelColor <- as.factor(Data$LabelColor)
p <- ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency, colour=LabelColor)) +
geom_bar(stat = "identity", size = 0) +
geom_text(size = 3, position = position_stack(vjust = 0.5)) +
scale_colour_manual(values = levels(Data$LabelColor)) +
scale_fill_manual(values = colorsPerCat) +
guides(colour = FALSE)