This question already has an answer here:
-
Overlay data onto background image
3 answers
I have a gif of a human body and need to plot a heat map on it . The data to be used is in R
I have found instructions on how to import a gif but it is the getting my data onto the gif that I am unsure of.
I presume I would need to put the gif on a grid at first and then get some co-ordinates and then assign the data to the relevant co-ordinates ?
For example if the head was located at (5,10), the stomach at (5,5) , the right knee at (4,3) etc etc on a grid .
Is there a package people use for this or is it just a task of programming strictly in R with no packages ? or do you use another resource ?
If you find an SVG image that you can use, you can also use the grImport
package to transform the file in an XML, which then makes it easy to modify with R.
For example, if you want to use this file, you can add some fill
shapes to the image using Illustrator/Gimp or anything else and then transform it to an XML using grImport
:
library(grImport)
PostScriptTrace("yourimage.ps")
This will create an yourimage.ps.xml
file. If you want you can modify the ids
of the fill path nodes in the XML to access them more easily to change the colors.
For example here, I made 14 body parts on the SVG file and changed their ids to names instead of number, you can find the XML here:
Human SVG with bodyparts XML
To change the colors of the bodyparts, you can just change the rgb
part of the fill
nodes:
library(grImport)
library(XML)
library(gridExtra)
#function to change the rgb color of the xml paths
changeColor<-function(bodypart,color){
node<-xpathSApply(doc, paste("//path[@id='",bodypart,"']/context/rgb",sep=""))[[1]]
rgbCol<-col2rgb(color)
xmlAttrs(node)["r"]=rgbCol[1]/255
xmlAttrs(node)["g"]=rgbCol[2]/255
xmlAttrs(node)["b"]=rgbCol[3]/255
}
#read the xml image
doc<-xmlParse("Human_body_front_and_side.ps.xml")
#these are the different parts you can change
bodyparts<-c("head","hand-right","hand-left","foot-left","foot-right","lowerleg-left","lowerleg-right",
"upperleg-left","upperleg-right","torso","forearm-right","forearm-left","upperarm-right","upperarm-left")
#color the bodyparts with random color
mapply(function(x,y){changeColor(x,y)},bodyparts,sample(colours(), 14))
#load the XML as a picture
body<-readPicture(saveXML(doc))
#plot it
grid.arrange(pictureGrob(body), ncol=1)
I get something like this:
You may find the grid package useful.