2016-09-14 115 views
0

我有一个数字为1-10的表格。它看起来像这样:R:基于给定值的表中的颜色单元格

My Table

现在我想以填补细胞用不同颜色对于每个整数。 例如,所有值为1的单元应该是红色,2个黑色等等。 你有什么建议如何实现这一目标? 非常感谢。

+0

目前尚不清楚你在找什么。你不能在R的表中“着色”。你是否希望在html表格,excel表格和其他表格格式中完成这项工作? –

+0

所以没有包可以按我想要的方式绘制一张桌子? –

+0

请解释您希望打印表格的格式。这里有一些关于如何制作一个[可重现的例子]的提示(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)值得它的名字。 –

回答

0

确实存在可以做到这一点的软件包。当前具有最直接接口的程序包可能是condformat,使用condformat::rule_file_discrete函数。不幸的是,我没有一个工作示例,因为我需要condformatrJava,这与我的系统不兼容。

pixiedust包(完全披露,我是作者)可以做到这一点,但目前并不是很直接。

library(pixiedust) 
library(scales) 
library(magrittr) 

# Make the table (as a matrix, but a data frame would work as well) 
set.seed(pi) 
X <- matrix(sample(1:10, 
        size = 100, 
        replace = TRUE), 
      nrow = 10) 

# Define 10 colors 
background <- hue_pal()(10) %>% 
    setNames(1:10) 

show_col(background) 


# Convert X to a dust object 
X_dust <- dust(X) 

# Apply the background colors 
for (i in sort(unique(as.vector(X)))){ 
    X_dust <- 
    sprinkle(X_dust, 
      rows = X_dust$body$row[X_dust$body$value == i], 
      cols = X_dust$body$col[X_dust$body$value == i], 
      bg = background[i], 
      fixed = TRUE) 
} 

# Print the HTML code 
X_dust %>% 
    sprinkle_print_method("html") 

我目前正在开发代码的代码只需要几行要做到这一点,但功能没有完全准备好释放呢。

+0

非常感谢你!这正是我一直在寻找的 –

相关问题