Add logo/img in ggplot

Are there other ways that are more elegant or more efficient?

Credit: markhw

library(tidyverse)
data("starwars")

# Contagem de espécies
species <- starwars %>% 
  count(species) %>% 
  filter(!is.na(species) & n > 1) %>% 
  arrange(-n) %>% 
  mutate(species = factor(species, species))

# Carregar logo PNG
logo <- grid::rasterGrob(
  png::readPNG("C:/Users/czarg/OneDrive/Documents/github/logo.png"),
  interpolate = TRUE)


# Gráfico de barras
ggplot(species, aes(x = species, y = n)) +
  geom_bar(stat = "identity") +
  theme_light() +
  coord_cartesian(clip = "off") +
  theme(plot.margin = unit(c(1, 1, 3, 1), "lines")) +
  annotation_custom(logo, xmin = 6.5, xmax = 8.5, ymin = -5, ymax = -8.5)

Since your question seems to imply it, I was curious to know what aspects of the code do you find inelegant or inefficient? That might help us answer what seems like a broad question.

This topic was automatically closed 90 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.