[R] use of writePNG() in a multiplot OR rasterImage() size control in a multiplot
Gygli, Gudrun
gudrun.gygli at wur.nl
Wed May 27 17:31:34 CEST 2015
Dear All,
I am trying to plot several things in a multiplot using par(mfrow...) AND add a pre-existing png file to that plot.
I have found 2 options of doing this in the web:
writePNG() and another fix using rasterImage().
writePNG(): the option using writePNG() does not add the " pre-existing png" to my mutliplot.
I use a code similar to this:
img<-read("myotherpng.png")
png(file="mydata.png")
par(mfrow=c(1,3))
plot(mydata)
boxplot(mydata)
writePNG(img, target="mydata.png")
Any ideas why this does notwork?
rasterImage(): this is a bit more tricky, but works partly:
the code comes from the rforge (https://www.rforge.net/doc/packages/png/readPNG.html)
and writes the png (see detailed code at the end of email), but is basically:
img<-read("myotherpng.png")
png(file="mydata.png")
par(mfrow=c(1,3))
plot(mydata)
boxplot(mydata)
rforgecode using rasterImage() to print img
If I use this code like below, I do get the" pre-existing png" plotted in my multiplot, but with very wrong dimensions.
rasterImage(image, xleft, ybottom, xright, ytop)
I have no idea how to get the xleft, ybottom,xright and ytop values that match the file I import and allow plotting it fitting into the space allocated by mfrow (meaning that the plot, boxplot and image do not overlap.)
Any ideas?
Thanks for any help about the topic.
Best regards
Gudrun
img<-read("myotherpng.png")
png(file="mydata.png")
par(mfrow=c(1,3))
plot(mydata)
boxplot(mydata)
if (exists("rasterImage")) { # can plot only in R 2.11.0 and higher
plot(1:2, type='n')
if (names(dev.cur()) == "windows") {
# windows device doesn't support semi-transparency so we'll need
# to flatten the image
transparent <- img[,,4] == 0
img <- as.raster(img[,,1:3])
img[transparent] <- NA
# interpolate must be FALSE on Windows, otherwise R will
# try to interpolate transparency and fail
rasterImage(img, 1.2, 1.27, 1.8, 1.73, interpolate=FALSE)
} else {
# any reasonable device will be fine using alpha
rasterImage(img, 1.2, 1.27, 1.8, 1.73)
}
}
Gudrun Gygli, MSc
PhD candidate
Wageningen University
Laboratory of Biochemistry
Dreijenlaan 3
6703 HA Wageningen
The Netherlands
Phone 31 317483387
e-mail: gudrun.gygli at wur.nl
- - - - - - - - - - - - - - - - - -
Project information: http://www.wageningenur.nl/en/show/Bioinformatics-structural-biology-and-molecular-modeling-of-Vanillyl-Alcohol-Oxidases-VAOs.htm
More information about the R-help
mailing list