Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a multiplot of data and images with ggplot?

Tags:

r

ggplot2

I would like to know if it is possible with ggplot2 to create a multiple plot (like a facet) including images to look like that:

What I would like

I don't know exactly how to arrange the image data to pass them to geom_raster() or how to include an image in a data frame...

what I have tried:

> img1 <- readPNG('1.png')
> img2 <- readPNG('2.png')
> img3 <- readPNG('3.png')

> test <- data.frame(c(1,2,3),c(5,2,7),c(img1,img2,img3))
> nrow(test)
 [1] 4343040

I already have a problem here to build a data frame with the images inside... each 3 lines a repeated (I guess once per pixel).

like image 594
Oneira Avatar asked Aug 08 '14 12:08

Oneira


People also ask

Can you make a table with ggplot?

This R package uses ggplot2 syntax to create great tables. for plotting. The grammar of graphics allows us to add elements to plots. Tables seem to be forgotten in terms of an intuitive grammar with tidy data philosophy – Until now.

What can I do with ggplot?

ggplot2 is a plotting package that provides helpful commands to create complex plots from data in a data frame. It provides a more programmatic interface for specifying what variables to plot, how they are displayed, and general visual properties.

Does ggplot work with Dataframe?

The dataframe is the first parameter in a ggplot call and, if you like, you can use the parameter definition with that call (e.g., data = dataframe ). Aesthetics are defined within an aes function call that typically is used within the ggplot call.

How do I plot multiple Ggplots together?

To arrange multiple ggplots on one single page, we'll use the function ggarrange()[in ggpubr], which is a wrapper around the function plot_grid() [in cowplot package]. Compared to the standard function plot_grid(), ggarange() can arrange multiple ggplots over multiple pages.


1 Answers

I went finally with this solution which decompose the plot to a gtable, add one more line and insert images. Of course once decompose one can't add more layer to the plot, so it should be the very last operation. I don't think the gtable can be converted back to plot.

library(png)
library(gridExtra)
library(ggplot2)
library(gtable)
library(RCurl) # just to load image from URL

#Loading images
img0 <- readPNG(getURLContent('http://i.stack.imgur.com/3anUH.png'))
img1 <- readPNG(getURLContent('http://i.stack.imgur.com/3anUH.png'))

#Convert images to Grob (graphical objects)
grob0 <- rasterGrob(img0)
grob1 <- rasterGrob(img1)

# create the plot with data
p <- ggplot(subset(mpg,manufacturer=='audi'|manufacturer=='toyota'),aes(x=cty,y=displ))+facet_grid(year~manufacturer)+geom_point()

# convert the plot to gtable
mytable <- ggplot_gtable(ggplot_build(p))

#Add a line ssame height as line 4 after line 3
# use gtable_show_layout(mytable) to see where you want to put your line
mytable <- gtable_add_rows(mytable, mytable$height[[4]], 3)
# if needed mor row can be added 
# (for example to keep consistent spacing with other graphs)

#Insert the grob in the cells of the new line
mytable <- gtable_add_grob(mytable,grob0,4,4, name = paste(runif(1)))
mytable <- gtable_add_grob(mytable,grob1,4,6, name = paste(runif(1)))

#rendering
grid.draw(mytable)

Result

Note: In this example, I use twice the same image, but it is of course possible to have as much as needed.

Inspired by: How do you add a general label to facets in ggplot2?

like image 84
Oneira Avatar answered Sep 28 '22 07:09

Oneira