Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggimage: Custom image from computer rather than from web

Tags:

r

ggplot2

ggimage

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)
like image 344
MYaseen208 Avatar asked Oct 22 '25 23:10

MYaseen208


1 Answers

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

like image 183
Scipione Sarlo Avatar answered Oct 25 '25 15:10

Scipione Sarlo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!