user13973103
user13973103

Reputation:

How to plot images as y-axis labels using ggtext?

I'm trying to plot some images (football logos) as my y-axis labels using ggtext. However, I get the below error message when I try and view the plot.

Error in function (type, msg, asError = TRUE) :

I've tried following some online examples on how to plot images using ggtext, so I'm a bit stuck on what is causing the error. Any help with this will be appreciated.

Thank you.

library(tidyverse)
library(ggalt)
library(ggtext)

data <- tibble(
  label = c("https://a.espncdn.com/i/teamlogos/nfl/500/tb.png",
            "https://a.espncdn.com/i/teamlogos/nfl/500/no.png",
            "https://a.espncdn.com/i/teamlogos/nfl/500/pit.png",
            "https://a.espncdn.com/i/teamlogos/nfl/500/ind.png",
            "https://a.espncdn.com/i/teamlogos/nfl/500/gb.png"),
  x1 = c(0.219, 0.169, 0.103, 0.193, 0.345),
  x2 = c(0.258, -0.030, 0.071, 0.315, 0.223)
)

link_to_img <- function(x, width = 30) {
  glue::glue("<img src='{x}' width='{width}'/>")
}

plot <- data %>%
  mutate(label = link_to_img(label)) %>%
  ggplot() +
  geom_dumbbell(aes(x = x1, xend = x2, y = label)) +
  theme(axis.text.y = element_markdown())

plot

Upvotes: 0

Views: 337

Answers (0)

Related Questions