4

The following graph can be produced from the R code given below. The images used in the graph are fetched from website. I'm wondering how to use images from computer.

enter image description here

library("ggplot2")
library("ggimage")

set.seed(2017-02-21)
d <- data.frame(x = rnorm(10),
                y = rnorm(10),
                image = sample(c("https://www.r-project.org/logo/Rlogo.png",
                                 "https://jeroenooms.github.io/images/frink.png"),
                               size=10, replace = TRUE)
                )

ggplot(d, aes(x, y)) + geom_image(aes(image=image), size=.05)
MYaseen208
  • 22,666
  • 37
  • 165
  • 309

1 Answers1

5

It is very simple, you have just to set the directory where you have saved the .png files you want to use:

library("ggplot2")
library("ggimage")
set.seed(2017-02-21)
d <- data.frame(x = rnorm(10),
                y = rnorm(10),
                image = sample(c("C:/YourDirectory/juventus.png",
                                 "C:/YourDirectory/sampdoria.png"),
                               size=10, replace = TRUE)
)

ggplot(d, aes(x, y)) + geom_image(aes(image=image), size=.05)

and you get the following result: enter image description here

Scipione Sarlo
  • 1,470
  • 1
  • 17
  • 31
  • Thanks @Scipione for useful answer. I'd appreciate if you guide me how to put one image as a single plot. I mean neither points nor labels but only one full plot size image. Thanks – MYaseen208 May 26 '18 at 19:17