rggplot2ggimage

How to use multiple colors with geom_image()


I'm having trouble figuring out how to highlight multiple colors with ggimage.

It's clear how one can set different images to a single color (from vignette):

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

set.seed(2017-02-21)
d <- data.frame(x = rnorm(10),
                y = rnorm(10),
                z = sample(c("A","B","C","A","B","C","A","B","C","A")),
                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), color="firebrick")

I introduced another column z into the example because the dataset I'm working with has a variable I would like apply the color aesthetic on.

I tried how one would typically set it in ggplot() (and/or geom_point()) call but I don't get my desired output:

ggplot(d, aes(x, y)) + geom_image(aes(image=image), color=d$z) 

Error in col2rgb(color) : invalid color name 'test'

It doesn't accept multiple colors?

ggplot(d, aes(x, y)) + geom_image(aes(image=image), color = c("blue","red","green"))

I also tried to specify three colors with RColorBrewer:

library(RColorBrewer)

my_color <- brewer.pal(3, "Set1")
ggplot(d, aes(x, y)) + geom_image(aes(image=image), color = my_color)

I would really appreciate your help trying to solve this.


Solution

  •     set.seed(2017-02-21)
        d <- data.frame(x = rnorm(10),
                    y = rnorm(10),
                    z = sample(c("A","B","C","A","B","C","A","B","C","A")),
                    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,group=z)) + geom_image(aes(image=image,color=factor(z)))