rggimage

How to add image to a ggplot


I wanted to add an image extracted from a webpage (LeBron_James assigned below) to the ggplot using ggimage package. How can i add it to the ggplot rscript below?

GGplot_mean_performance <- FirstPick_Wage_Performance %>% 
  ggplot(aes(x=Player, y=mean_performance, label=mean_performance))+ 
  geom_point(stat="identity", size=3)  +
  geom_hline(aes(yintercept = mean(mean_performance)), color = "blue", size = 3) + 
  geom_segment(aes(y = mean(mean_performance),             
                   x = Player, 
                   yend = mean_performance, 
                   xend = Player,)) +
  geom_text(color="red", size=4) +
  labs(title="Lollipop Chart of Player's mean performance for the first two years as draft pick \ncompared to mean performance of the population", 
       subtitle="Blake Griffin is the best performing 1st Pick and Anthony Bennett is the worst performing 1st Pick",
       caption="Source: https://www.basketball-reference.com & https://basketball.realgm.com") + 
  ylim(20, 80) +
  coord_flip() +
  theme(legend.position = "none")

Lebron_James <- image_read2("https://nba-players.herokuapp.com/players/james/lebron")

Solution

  • This will plot the image instead of a point

    library("ggplot2")
    library("ggimage")
    
    
    d <- data.frame(x = 1:2,
                    y = 1:2,
                    image = c("https://nba-players.herokuapp.com/players/james/lebron",
                              "https://nba-players.herokuapp.com/players/james/lebron"))
    
    ggplot(d, aes(x, y)) + 
      geom_image(aes(image=image), size=.2) +
      scale_x_continuous(limits = c(0, 3)) +
      scale_y_continuous(limits = c(0,3))