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: