ggplot - Adjust label colors and backgrounds for geom_line layered over geom_bar

Here is the setup:

library(tidyverse)
library(reprex)

d <- structure(list(Month = structure(c(3L, 3L, 3L, 4L, 4L, 4L, 
5L, 5L, 5L), .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), .Label = c("A", 
"B", "C"), class = "factor"), Amt.x = c(2967890564, 1381345923, 
511874052, 1622821062, 998569432, 407863612, 1837818542, 
1076827359, 461121680), Score = c(810, 710, 657, 807, 
709, 656, 807, 709, 655), Amt.y = c(4861110539, 4861110539, 
4861110539, 3029254106, 3029254106, 3029254106, 3375767581, 
3375767581, 3375767581), Score_Total = c(766, 766, 766, 
755, 755, 755, 755, 755, 755)), row.names = c(NA, -9L), class = "data.frame")

ggplot(data = d, aes(x=Month, y = Score, fill = Grade)) + 
  geom_bar(stat = "identity", position = position_dodge()) +
  coord_cartesian(ylim=c(550, 850)) +
    geom_text(aes(label = Score, colour = Grade),
              position=position_dodge(width=0.9), 
              hjust=-.5,
              size = 3.5, 
              show.legend = FALSE, 
              fontface="bold", angle=90) +
  labs(title = "NA",
       subtitle = "NA") +
  theme_bw() +
  guides(fill = guide_legend(reverse = F)) +
  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)) +
  scale_color_manual(values = c("black", "black", "black")) +
  geom_line(aes(x = Month, y = Score_Total, group=1), 
            size=2,
            colour="red") + 
  geom_text(aes(label = Score_Total, 
                 x = Month, 
                 y = Score_Total), 
                 size = 3)

Question:
How can I change the labels for the red line so that the labels are slightly above or below the line, centered appropriately, and the font is red and sightly larger?

Thanks!Preformatted text

Is this what you mean?

ggplot(data = d, aes(x=Month, y = Score, fill = Grade)) +
    geom_bar(stat = "identity", position = position_dodge()) +
    coord_cartesian(ylim=c(550, 850)) +
    geom_text(aes(label = Score, colour = Grade),
              position=position_dodge(width=0.9),
              hjust=-.5,
              size = 3.5,
              show.legend = FALSE,
              fontface="bold", angle=90) +
    labs(title = "NA",
         subtitle = "NA") +
    theme_bw() +
    guides(fill = guide_legend(reverse = F)) +
    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)) +
    scale_color_manual(values = c("black", "black", "black")) +
    geom_line(aes(x = Month, y = Score_Total, group=1),
              size=2,
              colour="red") +
    geom_text(aes(label = Score_Total,
                  x = Month,
                  y = Score_Total),
              vjust = -1,
              color = "red",
              size = 4)

1 Like

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

Yes, you can learn more by reading this FAQ

Also, if you use the reprex package you would get already formatted code and plots that you can paste directly into the forum, you can learn how to use it by reading this other FAQ

1 Like

That is perfect! Thanks very much.
How did you paste that code in your response so that it was presented as code (in grey and better looking that the reprex I posted initially?) Do I need to surround with ``` or something?

Thanks again!

Thanks - I used the reprex (or tried to) but wasn't getting the dataframe d to work. I'll keep learning and improving my posts. Thanks for the help!