5

When researching this answer, I tried to draw the image strip via geom_image() from the ggimage package but couldn't get it to work. geom_image() modifies the images aspect ratios, and I don't know how to prevent it from doing it (or whether that is even possible). It's also not clear to me in what units size is measured. From how the code behaves, maybe it's in npc coordinates running from 0 to 1, regardless of the ggplot2 coordinate system?

Here is the code I used:

require(ggimage)
df_img <- data.frame(phase = c("Interphase", "Prophase", "Metaphase", "Anaphase", "Telophase"),
                     image = c("http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_interphase.jpg",
                               "http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_prophase.jpg",
                               "http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_metaphase2.jpg",
                               "http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_anaphase2.jpg",
                               "http://www.microbehunter.com/wp/wp-content/uploads/2009/lily_telophase.jpg"))
df_img$phase <- factor(df_img$phase, levels=df_img$phase)

ggplot(df_img, aes(x = phase, y = 0, image = image)) + geom_image(size = 0.18)

And this is the resulting image:

enter image description here

This is what the image should look like:

enter image description here

Note: This is a question specifically about the behavior of ggimage. I know how to generate the correct image using other approaches, e.g. by using draw_image() from cowplot.

www
  • 38,575
  • 12
  • 48
  • 84
Claus Wilke
  • 16,992
  • 7
  • 53
  • 104

2 Answers2

6
ggplot(df_img, aes(x = phase, y = 0.25, image = image)) +
  geom_image(size = 0.5, by="height")+
  scale_size_identity()

will produce

enter image description here

If by is specified, size is mapped and interpreted as npc in the data space, either as width or height, and the aspect ratio maintained for the current device (not after resizing). If size is Inf, the picture stretches to fill the whole panel.

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Re: reading images from URL: I think both approaches have merit. If you want to use images instead of points, where you’ll draw the same image multiple times, then clearly re-reading it every time is bad. However, for the use case of only adding a few images to a plot, as here, asking the user to set up a separate scale that maps values to image files seems overly complicated. – Claus Wilke Nov 24 '17 at 22:26
  • 1
    I take your statement of "not after resizing" implies the answer to my question is "no". Thus I'm going ahead and accept your answer. – Claus Wilke Nov 30 '17 at 19:35
2

A rather "raw" solution:

p <- ggplot(df_img, aes(x = phase, y = 0, image = image)) + 
     geom_image(size = 0.18) +  coord_fixed()

g <- ggplotGrob(p)   
for (k in 1:length(g$grobs[[6]]$children[[3]]$children)) {
  g$grobs[[6]]$children[[3]]$children[[k]]$height <- unit(0.8,"native")
}
library(grid)
grid.draw(g)

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58