ggplot and strange text on legend after using geom_text

Hello,
I'm plotting 2 series of data using geom_line but I have a minor error.
If you run the code, You can see that the legend shows some "a" for each line plotted.
I don't know where that text comes from.
It appears only when I add the geom_text.
Is there any way to show the legend without that annoying letter "a"?
I asked geminis.google.com but It failed to solve this issue.
Thanks for your time and interest.
Have a nice weekend.

library(tidyverse)
library(scales)

world_bank_pop %>% 
  pivot_longer(cols = `2000`:`2017`,
               names_to = "year",
               values_to = "value") %>% 
  filter(country %in% c("CHL", "ECU"), indicator == "SP.POP.TOTL") %>% 
  mutate(year = as.numeric(year)) %>%
  ggplot(aes(x = year, y = value, color = country)) +
  geom_line() +
  geom_point(shape=23, color="black", fill="white", alpha=0.5)+
  scale_y_continuous(
    labels = label_number(decimal.mark = ",",big.mark = "."),
    breaks = seq(13e6,19e6,0.5e6),
    name="Población"
)+
  geom_text(aes(label = format(round(value/1e3, 0), big.mark = ".", scientific = FALSE)), 
            vjust = -0.5, nudge_y = +1,family = "Gentium" )+
  scale_x_continuous(
    breaks = 2000:2017,
    name="Años"
  ) + 
  scale_color_manual(values = c("CHL" = "blue", "ECU" = "red"), 
                     labels = c("CHL" = "Chile", "ECU" = "Ecuador"))

The "a" in the legend is intended to show the coloring of the text. You can suppress it by setting show.legend =FALSE in geom_text()

geom_text(aes(label = format(round(value/1e3, 0),big.mark = ".", 
                               scientific = FALSE)), 
            show.legend = FALSE, 
            vjust = -0.5, nudge_y = +1)
1 Like

The strange text in the legend after using geom_text() in ggplot is likely due to aes() mapping inside geom_text(). Try setting show.legend = FALSE in geom_text() or ensure the aesthetic mappings are correct.

1 Like

It wasn't easy to find such answer searching through google or using AI.
Thanks for your replies, people.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.