I want to add custom label colors to a bar plot. By adding a colour aes inside a geom_text() layer, the position alignment of the labels gets loss.
Is something I'm missing? It's a bug?
library(dplyr, warn.conflicts = FALSE) library(ggplot2) df <- mtcars %>% count(across(c(cyl, gear), as.factor)) %>% mutate(text_colour = if_else(gear == 3, 'white', 'blue')) g <- ggplot(data = df, aes(x = cyl, y = n, fill = gear)) + geom_col() # Reference g + geom_text(aes(label = n), position = position_stack(vjust = 0.5))
# Expected output: Labels at the same position as previous plot. # The labels don't respect the position argument. g + geom_text(aes(label = n, colour = text_colour), position = position_stack(vjust = 0.5)) # Here should go a `scale_colour_identity()` scale. It's not necessary to show my problem.
Created on 2021-01-11 by the reprex package (v0.3.0)