在数据帧R中包含图像

时间:2016-05-03 19:31:44

标签: r graphics

我在R中有一个数据框,类似这样

       Score   Result
1       1       Pass       
2       0.8     Pass       
3       1       Pass       
4       0.5     Fail        
5       0.3     Fail        
6       0.2     Fail

根据结果(通过或失败)的值,我想在将数据框写入PDF时显示大拇指或大拇指向下的图像。

我不想使用针织或闪亮。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您可以在grid.table,

中定义自己的单元格函数

enter image description here

d <- read.table(textConnection("Score   Result
1       1       Pass       
2       0.8     Pass       
3       1       Pass       
4       0.5     Fail        
5       0.3     Fail        
6       0.2     Fail"), stringsAsFactors=FALSE)

library(gridExtra)
library(grid)
library(png)
up <- rasterGrob(readPNG("up.png"), height = unit(1,"line"))
down <- rasterGrob(readPNG("down.png"), height = unit(1,"line"))

cell_grob <- function (label, parse = FALSE, col = "black", fontsize = 12, ...) 
{
  switch(label, "Pass" = up, "Fail" = down, 
         grid::textGrob(label = label, ...))

}

custom <- ttheme_default(core = list(fg_fun = cell_grob))
grid.newpage()
grid.table(d, theme = custom)