我需要导入多个图像并呈现拼贴输出。我在想的是做一个树形图,图像大小映射到角色的分数。
df <- data.frame(character=c("Homer","Marge", "Bart", "Lisa","Maggie", "Moe", "Blinky","Bumblebee Man","Duffman","Maude Flanders","Ned Flanders","Rod Flanders","Todd",Jimbo","Otto Mann","Snowball"),score=c(268,267,495, 432, 219, 373, 152, 356, 461, 116,107,165, 305,228, 461, 608))
之前我曾使用过treemap,还有ggplot geom_tile,但我不确定是否可以在tile中生成图片。我甚至不知道如何将图像插入到我的数据框中以进行绘图...
答案 0 :(得分:1)
不是真正的答案,而是快速演示如何开始完成任务。
library(treemap)
library(gridSVG)
library(XML)
df <- data.frame(
character=c("Homer","Marge", "Bart", "Lisa","Maggie", "Moe", "Blinky","Bumblebee Man","Duffman","Maude Flanders","Ned Flanders","Rod Flanders","Todd","Jimbo","Otto Mann","Snowball")
,score=c(268,267,495, 432, 219, 373, 152, 356, 461, 116,107,165, 305,228, 461, 608)
)
tm <- treemap( df, index = "character", vSize = "score" )
svg <- grid.export()$svg
# see http://stackoverflow.com/questions/10688516/fill-svg-path-with-a-background-image-without-knowing-heightwidth?rq=1
pattern <- newXMLNode(
"defs"
,.children = list(
newXMLNode(
"pattern"
, attrs = c(
id = "img_homer"
,patternUnits="userSpaceOnUse"
,patternTransform="translate(0, 0) scale(1, -1) rotate(0)"
,width="106"
,height="98"
)
, .children = newXMLNode(
"image"
, attrs = c(
"xlink:href" = "http://i.imgur.com/JP4s21O.jpg"
,width = 106
,height = 80
)
)
)
)
)
addChildren( svg, pattern )
homer <- getNodeSet(
getNodeSet( svg, "//*[contains(@id,'data.2')]")[[1]]
,"//*[local-name()='rect']"
)[[5]]
homer_attrs <- xmlAttrs(homer)
homer_attrs[["fill"]] <- "url(#img_homer)"
xmlAttrs(homer) <- homer_attrs
library(htmltools)
browsable(HTML(saveXML(svg)))