Strange symbols in plot legend

Anyone know why the legend is showing some strange letter a's (it would seem)?

dat6<-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, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
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, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("A", 
"B", "C", "Total"), class = "factor"), Percent_with_Co = c(63, 
27, 10, 56, 33, 12, 57, 30, 13, 61, 29, 11, 64, 27, 9, 65, 26, 
9, 64, 26, 10, 64, 26, 10, 65, 25, 10, 65, 25, 10, 65, 25, 10, 
69, 22, 9, 66, 23, 10, 65, 23, 12, 62, 27, 11, 64, 25, 10, 41, 
43, 42, 45, 46, 42, 40, 41, 42, 40, 40, 40, 38, 37, 39, 40)), row.names = c(NA, 
-64L), class = "data.frame")

ggplot(data = dat6, aes(x=Vintage, y = Percent_with_Co, group = Grade)) +
  geom_line(aes(color=Grade), size=1.25) +
  scale_color_manual(values=c("black", "gray40", "grey", "red")) +
  labs(title = "Title") +
  theme_bw() +
  ylim(0,80) +
  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()) +
    theme(axis.text.x = element_text(angle = 90),
        axis.text.x.bottom = element_text(vjust = 0.5)) +
  geom_text(aes(label = paste0(Percent_with_Co,"%"),
                x = Vintage,
                y = Percent_with_Co, 
                color=Grade),
                vjust = -1,
                hjust = .2,
                size = 3)

Was able to find a solution. See below.

Needed to add "show.legend = FALSE"

ggplot(data = dat6, aes(x=Vintage, y = Percent_with_Co, group = Grade)) +
  geom_line(aes(color=Grade), size=1.25) +
  scale_color_manual(values=c("black", "gray40", "grey", "red")) +
  labs(title = "Title") +
  theme_bw() +
  ylim(0,80) +
  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()) +
    theme(axis.text.x = element_text(angle = 90),
        axis.text.x.bottom = element_text(vjust = 0.5)) +
  geom_text(aes(label = paste0(Percent_with_Co,"%"),
                x = Vintage,
                y = Percent_with_Co, 
                color=Grade),
                vjust = -1.5,
                hjust = .2,
                size = 3, 
            fontface = "bold",
            show.legend = FALSE)

I didn't know you could do that. Good work.

1 Like

It is unintuitive as I obviously wanted to show the legend but that command took care of it (guessing there is a legend method for geom_text which was being added to the geom_line legend.)

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