Hide part of a chart caption

Asked

Viewed 46 times

-2

I have a chart made with ggplot2, in the legend is appearing a size and I’d like to remove it, but I don’t know how.

My code:

updates %>%
    mutate(mortalidade = deaths / confirmed) %>%
    filter(iso3c %in% paises, date == max(date) - 1) %>%
    arrange(desc(mortalidade)) %>%
    select(country, mortalidade, region) %>%
    na.omit() %>%
    ggplot() +
    geom_col(aes(x = reorder(country, - mortalidade), y = mortalidade, fill = na.omit(region))) +
    geom_text(aes(x = country, y = mortalidade, label =  percent(round(mortalidade, 2)),
              vjust = -0.15, size = 3)) +
    scale_y_continuous(labels = percent_format(accuracy = 1)) +
    scale_fill_economist() +
    labs(x = "", y = "", fill = "") +
    theme(legend.position = "bottom",
          text = element_text(size = 10), axis.text.x = element_text(angle = 90, hjust = 1),
          panel.background = element_rect(fill = "white", colour = "grey10"),
          panel.grid.major = element_line(colour = "gray", linetype = "solid"))

My chart: inserir a descrição da imagem aqui

I’d like to withdraw that size A3. If you think it necessary, I’ll put the dput() of my data.

  • 2

    Other users should be able to copy and run the code you posted without having to request extra data. If you are not using an R database, always include the data used.

1 answer

3


ggplot includes in the captions all the variables specified within the aesthetics (except the axes). If the value used for some form (in your case, text size) is constant or independent of some variable from your dataset, put it outside the aes:

library(ggplot2)

dados <- diamonds[1:4,]

ggplot(dados, aes(clarity, table)) +
  geom_col(aes(fill = cut)) +
  geom_text(aes(label = carat), size = 4, nudge_y = 3)

inserir a descrição da imagem aqui

If you need to map a variable to a geometry but you don’t want it to appear in the caption, use the option show.legend = FALSE. For example:

ggplot(dados, aes(clarity, table)) +
  geom_col(aes(fill = cut), show.legend = FALSE)
  • 1

    Another option is to remove vjust = -0.15, size = 3 of aes(). As they do not depend on base variables are not necessary there and no longer have a legend of their own.

  • Yes, that’s what I wanted to demonstrate, only using a generic example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.