Changing color of labels in line chart (geom_text)

Does anyone know how to change the red color labels to match the bars below them? For example, the far left 2.1 should be black, the 3.3 should be dark grey, and the 8.9 should be light grey (and the pattern should continue for the entire chart.) I feel like it has something to do with there being 3 levels 48 rows - I've tried interpresting the error message (for example, Aesthetics must be either length 1 or the same as the data) by moving the color=c("red","red","blue") outside of the aes but that doesn't seem to work. I've also tried scale_fill_manual as show below...


dat13 <- structure(list(Vintage = structure(c(3L, 3L, 3L, 4L, 4L, 4L, 
5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 
10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L, 
14L, 15L, 15L, 15L, 16L, 16L, 16L, 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("Pilot", 
"2H2018", "201901", "201902", "201903", "201904", "201905", "201906", 
"201907", "201908", "201909", "201910", "201911", "201912", "202001", 
"202002"), class = "factor"), Grade = structure(c(1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("A", 
"B", "C"), class = "factor"), Percent = c(1.4, 2.6, 8, 1.9, 3.9, 
10.6, 2, 3.7, 10.6, 1.4, 3.1, 9.5, 1.1, 2.5, 8.9, 1.2, 2.5, 9.1, 
1.2, 2.5, 9.5, 1.3, 2.3, 8.9, 1.2, 2.3, 9.1, 1.4, 2.2, 9.5, 1, 
2.4, 9.5, 0.9, 1.9, 8.5, 0.9, 2.2, 9.1, 1.3, 2.5, 10.8, 1.7, 
3.1, 9.5, 2.1, 3.3, 8.9)), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -48L), groups = structure(list(
    Vintage = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 
    4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 
    9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 
    13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L
    ), .Label = c("Pilot", "2H2018", "201901", "201902", "201903", 
    "201904", "201905", "201906", "201907", "201908", "201909", 
    "201910", "201911", "201912", "202001", "202002"), class = "factor"), 
    Grade = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("A", "B", "C"
    ), class = "factor"), .rows = list(46L, 47L, 48L, 43L, 44L, 
        45L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
        13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
        24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
        35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L)), row.names = c(NA, 
-48L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

ggplot(data = dat13, aes(x=Vintage, y = Percent, fill = Grade)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  geom_text(aes(label = Percent),
            color=c("red"),
            position=position_dodge(width=1),
            hjust=-.25,
            size = 3,
          show.legend = FALSE, 
          fontface="bold", 
          angle=90) +
        labs(title = "NA") +
  theme_bw() + 
  ylim(0,15) +
  theme(legend.position = "bottom", 
        legend.direction = "horizontal",
        legend.title = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank()) + 
  scale_fill_manual(values = c("black", "gray40", "grey")) +
    theme(axis.text.x = element_text(angle = 90),
        axis.text.x.bottom = element_text(vjust = 0.5))

Got it!


ggplot(data = dat13, aes(x=Vintage, y = Percent, fill = Grade)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  geom_text(aes(label = Percent, color=Grade),
            position=position_dodge(width=1),
            hjust=-.25,
            size = 3,
          show.legend = FALSE, 
          fontface="bold", 
          angle=90) +
        labs(title = "NA") +
  theme_bw() + 
  ylim(0,15) +
  theme(legend.position = "bottom", 
        legend.direction = "horizontal",
        legend.title = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank()) + 
  scale_fill_manual(values = c("black", "gray40", "grey")) +
  scale_color_manual(values = c("black", "gray40", "grey")) +
    theme(axis.text.x = element_text(angle = 90),
        axis.text.x.bottom = element_text(vjust = 0.5))
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.