3

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.

  • Are you after `ggplot(d, aes(x, y)) + geom_image(aes(image=image, color = z))`? – hpesoj626 May 23 '18 at 03:42
  • 3
    if i'm reading geom_image correctly (https://github.com/cran/ggimage/blob/master/R/geom_image.R) the routine does not support color as an aesthetic so the color param outside of aes applies one color as you've shown. Typically a geom would use your z column inside aes to apply different colors but this doesn't look like it's available for geom_image. A possible alternative would be to draw a circle or rectangle behind the image with your color value (perhaps by calling geom_point before calling geom_image) – John Walker May 23 '18 at 03:44
  • I tried looking at the source code too (with `edit(getAnywhere('geom_image'))`) but I couldn't make too much sense of the code. Thanks for clearing this up, I'll open an issue on the Github page. – Matthew J. Oldach May 23 '18 at 04:42

1 Answers1

1
    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)))
  • 3 years + 4 months - "Better late than never" Thanks for the answer :) Once you've passed factor(z) to the color param you can customize your colors via the categories (here A,B + C) ggplot(d, aes(x, y,group=z)) + geom_image(aes(image=image,color=factor(z))) + scale_color_manual(values = c("C" = "green", "A" = "pink", "B" = "orange")) – Matthew J. Oldach Oct 14 '21 at 16:02