Supeimposing Plot on Raster Image

1,106 views
Skip to first unread message

Lorenzo Isella

unread,
Sep 10, 2012, 9:36:24 AM9/10/12
to ggplot2
Dear All,
I am used to producing R plots with different packages (though I rely
most of the time on ggplot2).
For some presentation, it would be nice to overlay one R-generated
plot on a custom picture (a jpeg or png file most of the time) which
is NOT generated via an R script.
I wonder if this can be done from inside R i.e. if I can generate a
plot e.g. with ggplot2 and then read and use as a background to the
plot an image file.
I found some online discussion about this

http://finzi.psych.upenn.edu/R/Rhelp02a/archive/66704.html
http://journal.r-project.org/archive/2011-1/RJournal_2011-1_Murrell.pdf
http://stackoverflow.com/questions/10318868/how-to-overlay-level-plots-in-r

but none of them involves ggplot2.
I found something about geographical maps and ggplot2, but I need
something flexible enough to superimpose a ggplot2 plot on a
non-vectorial image.
Any suggestion (and above all minimal example) would help me a lot.
Cheers



Lorenzo

Roman Luštrik

unread,
Sep 10, 2012, 9:44:29 AM9/10/12
to Lorenzo Isella, ggplot2
If you mean by adding an image as a background, see one solution here:


Cheers,
Roman






Lorenzo

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2



--
In God we trust, all others bring data.

Lorenzo Isella

unread,
Sep 10, 2012, 6:06:13 PM9/10/12
to Roman Luštrik, ggplot2
Many thanks with your suggestion.
The example works like a charm, but when I try to read my own png file, I
end up in trouble.
In the example script (I copy and paste what I found in your link)


###########################################
library(png)
library(gridExtra)
m <- readPNG(system.file("img", "Rlogo.png", package="png"), FALSE)
w <- matrix(rgb(m[,,1],m[,,2],m[,,3], m[,,4] * 0.2), nrow=dim(m)[1]) #0.2
is alpha


qplot(1:10, rnorm(10), geom = "blank") +
annotation_custom(xmin=-Inf, ymin=-Inf, xmax=Inf, ymax=Inf,
rpatternGrob(motif=w, motif.width = unit(1, "cm"))) +
geom_point()

#############################################

m is an array having dimensions (x,y,4).
When I read my own png image

m <- readPNG("myfile.png", FALSE)

I end up with an array m whose dimensions are (x,y,3), hence the script
crashes because m[,,4] cannot be addressed.
I must be doing something trivially wrong, but I am puzzled.
Any suggestion is appreciated.
Cheers

Lorenzo

Lorenzo Isella

unread,
Sep 10, 2012, 6:06:14 PM9/10/12
to Roman Luštrik, ggplot2
Thanks a lot.
I have a problem, though.
I copy and paste below the script that you refer to

######################################
library(png)
library(gridExtra)
m <- readPNG(system.file("img", "Rlogo.png", package="png"), FALSE)
w <- matrix(rgb(m[,,1],m[,,2],m[,,3], m[,,4] * 0.2), nrow=dim(m)[1]) #0.2
is alpha


qplot(1:10, rnorm(10), geom = "blank") +
annotation_custom(xmin=-Inf, ymin=-Inf, xmax=Inf, ymax=Inf,
rpatternGrob(motif=w, motif.width = unit(1, "cm"))) +
geom_point()
######################################

which does work.
However, when I read my own image file with


m <- readPNG("many-countries.png", FALSE)

it crashes. The reason is that now m is an array with dimension (x,y,3)
(whereas in the example script is has dimensions (x,y,4)).
Where does this difference come from?
I though that an RGB image is a combination of 3 array and I do not
understand how to add a 4th one.
Any suggestions?
Cheers

Lorenzo



On Mon, 10 Sep 2012 15:44:29 +0200, Roman Luštrik
<roman....@gmail.com> wrote:

Roman Luštrik

unread,
Sep 11, 2012, 2:48:48 AM9/11/12
to Lorenzo Isella, ggplot2
Without seeing your code and the image you're using it's hard for me to say what's going astray.

Cheers,
Roman



Jean-Olivier Irisson

unread,
Sep 11, 2012, 5:01:05 AM9/11/12
to Lorenzo Isella, Roman Luštrik, ggplot2
On 2012-Sep-11, at 00:06 , Lorenzo Isella <lorenzo...@gmail.com> wrote:
>
> I end up with an array m whose dimensions are (x,y,3), hence the script crashes because m[,,4] cannot be addressed.

No alpha channel in your PNG? dimensions 1, 2 and 3 are probably R, G, B and dimension 4 would be the alpha (transparency) channel. Just put a fixed value in there:

w <- matrix(rgb(m[,,1], m[,,2], m[,,3], 0.2), nrow=dim(m)[1])

or

w <- matrix(rgb(m[,,1], m[,,2], m[,,3], rep(0.2, times=length(m[,,3])), nrow=dim(m)[1])

if the code needs vectors of the same length.

Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/
Send me large files at: http://jo.irisson.com/upload/

Lorenzo Isella

unread,
Sep 11, 2012, 1:58:48 PM9/11/12
to Jean-Olivier Irisson, Roman Luštrik, ggplot2
Hello,
First of all thanks to you and Roman for helping me out with this.
I think I have finally sorted out the situation.
Please find below at the end of the email a short snippet which does what
I need (at least for the case of a png file without alpha channel).
Cheers

Lorenzo

#####################################################

library(png)
library(gridExtra)
library(grid)
library(ggplot2)


m <- readPNG("myfile.png", FALSE)
w <- matrix(rgb(m[,,1], m[,,2], m[,,3], 0.5), nrow=dim(m)[1])

g <- qplot(1:10, rnorm(10), geom = "blank") +
annotation_custom(xmin=-Inf, ymin=-Inf, xmax=Inf, ymax=Inf,
rasterGrob(w)) +
geom_point()


pdf("g.pdf")
print(g)
dev.off()

#########################################################
Reply all
Reply to author
Forward
0 new messages