如何在R上基于人体的gif绘制热图? [英] How to make a heat map in R based on a gif of the human body?

查看:134
本文介绍了如何在R上基于人体的gif绘制热图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gif的人体,需要在其上绘制热图。我在R



中找到了要使用的数据,但是我不确定如何导入gif,但这是我将数据导入到gif中的方式。 / p>

我想我首先需要将gif放在网格上,然后获取一些坐标,然后将数据分配给相关的坐标?



例如,如果头部位于(5,10),胃位于(5,5),右膝位于(4,3)等,在网格上等等。



人们是否为此使用了一个软件包,或者仅仅是在R中没有任何软件包严格地进行编程的任务?还是使用其他资源?

解决方案

如果找到可以使用的SVG图像,也可以使用 grImport 包将文件转换成XML,然后可以很容易地用R修改。



例如,如果您要使用此文件,则可以添加一些使用Illustrator / Gimp或其他任何东西填充形状到图像,然后然后对其进行转换使用 grImport

  library(grImport )
PostScriptTrace( yourimage.ps)

这将创建 yourimage.ps.xml 文件。如果需要,可以修改XML中填充路径节点的 ids ,以便更轻松地访问它们以更改颜色。



例如,在这里,我在SVG文件上制作了14个身体部位,并将其id更改为名称而不是数字,您可以在此处找到XML:
带有人体器官XML的人类SVG



要更改人体器官的颜色,您只需更改 fill 节点的 rgb 部分:

 库(grImport)
库(XML)
库(gridExtra)

#用于更改xml路径
的rgb颜色的函数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
}

#读取xml图像
doc< -xmlParse( Human_body_front_and_side.ps.xml)

#这些是可以更改的不同部分
bodyparts< -c( head, hand-right, hand-left, foot-left, foot-right, lowerleg-left ,下腿右,
上腿左,上腿右,躯干,前臂右,前臂左​​,上臂右,上臂左)

#使用随机颜色
为身体部位着色
mapply(function(x,y){changeColor(x,y)},bodyparts,sample(colours(),14))


#将XML加载为图片
body <-readPicture(saveXML(doc))

#将其绘制为
grid.arrange (pictureGrob(body),ncol = 1)

我得到这样的东西:




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:

这篇关于如何在R上基于人体的gif绘制热图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆