条件格式化:使单元格丰富多彩

jon*_*jon 8 excel formatting r

是否可以执行以下操作:

loc1 <- c("Aa", "Aa", "aa", "Aa")
loc2 <- c("aa", "aa", "aa", "AA")
loc3 <- c("aa", "Aa", "aa", "aa")
gen <- data.frame(loc1, loc2, loc3)

loc1g <- c(0.01, 0.5, 1, 0.75)
loc2g <- c(0.2, 0.1, 0.2, 0.6)
loc3g <- c(0.8, 0.8, 0.55, 1)
pval <- data.frame(loc1g, loc2g, loc3g)
Run Code Online (Sandbox Code Playgroud)

我想打印到文件到gen数据帧,这是由pval数据帧有条件地格式化的.比颜色(row1,col1)的平均值取决于pvale(row1,col1).以下是颜色编码:

0 to 0.3   is "red" text color 
0.31 to 0.7 is "yellow"
> 0.7  is "red" 
Run Code Online (Sandbox Code Playgroud)

gen [1,1]将以红色文字颜色打印"Aa"等等....

感谢你的帮助.

EDITS:

我更感兴趣的是打印而不是绘图.如果我可以将输出保存为MS excel并在MSEXCEL中打开它会很棒.我也可以是其他类型的文本编辑器格式,可以读取彩色编码文本.由于我的原始数据矩阵的尺寸应为1000 x 1000甚至更大.我想快速了解每个gen类别的p值.

Joh*_*lby 5

听起来你想模仿Excel.以下是几个例子:

x = 1:ncol(pval)
y = 1:nrow(pval)

# Colored backgrounds
dev.new(width=4, height=4)
image(x, y, t(as.matrix(pval)),
  col = c('red', 'yellow', 'red'),
  breaks = c(0, 0.3, 0.7, 1),
  xaxt='n', 
  yaxt='n', 
  ylim=c(max(y)+0.5, min(y)-0.5), 
  xlab='', 
  ylab='')
centers = expand.grid(y, x)
text(centers[,2], centers[,1], unlist(gen))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

# Colored text
dev.new(width=4, height=4)
image(x,y, matrix(0, length(x), length(y)),
  col='white',
  xaxt='n', 
  yaxt='n', 
  ylim=c(max(y)+0.5, min(y)-0.5), 
  xlab='', 
  ylab='')
pvals = unlist(pval)
cols = rep('red', length(pvals))
cols[pvals>0.3 & pvals<=0.7] = 'yellow'
text(centers[,2], centers[,1], unlist(gen), col=cols)
grid(length(x),length(y))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


dar*_*zig 4

给出一个类似 POC 的答案,它使用了一个丑陋的循环,而不是最漂亮的设计:

正在加载例如。该xlxs包能够写入 Excel 2007 格式:

library(xlsx)
Run Code Online (Sandbox Code Playgroud)

让我们创建一个工作簿和一个工作表(请参阅手册!):

wb <- createWorkbook()
sheet <- createSheet(wb, "demo")
Run Code Online (Sandbox Code Playgroud)

定义一些要在电子表格中使用的样式:

red <- createCellStyle(wb, fillBackgroundColor="tomato", fillForegroundColor="yellow", fillPattern="BIG_SPOTS")
yellow <- createCellStyle(wb, fillBackgroundColor="yellow", fillForegroundColor="tomato", fillPattern="BRICKS1")
Run Code Online (Sandbox Code Playgroud)

还有一个丑陋的循环,它将每个单元格以适当的格式粘贴到电子表格中:

for (i in 1:nrow(pval)) {
    rows <- createRow(sheet, rowIndex=i)
    for (j in 1:ncol(pval)) {
        cell.1 <- createCell(rows, colIndex=j)[[1,1]]
        setCellValue(cell.1, gen[i,j])
        if ((pval[i,j] < 0.3) | (pval[i,j] > 0.7)) {
            setCellStyle(cell.1, red)
        } else {
            setCellStyle(cell.1, yellow)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

保存 Excel 文件:

saveWorkbook(wb, '/tmp/demo.xls')
Run Code Online (Sandbox Code Playgroud)

结果:演示.xls


带包的替代解决方案ascii

ascii.data.frame()可以将数据帧导出为多种格式,并能够添加一些格式。例如导出到pandoc,首先将每个单元格的样式定义为具有相同尺寸的数组pval

style <- matrix('d', dim(pval)[1], dim(pval)[2])
style[pval < 0.3 | pval > 0.7] <- 's'
Run Code Online (Sandbox Code Playgroud)

设置所需的输出:

options(asciiType = "pandoc")
Run Code Online (Sandbox Code Playgroud)

并导出数据框:

> ascii(gen, style=cbind('h', style))

    **loc1**   **loc2**   **loc3**  
--- ---------- ---------- ----------
1   Aa         **aa**     **aa**    
2   **Aa**     **aa**     Aa        
3   **aa**     aa         **aa**    
4   **Aa**     **AA**     **aa**    
--- ---------- ---------- ----------
Run Code Online (Sandbox Code Playgroud)

ascii::Report可以轻松地将其转换为 pdf、odt 或 html。试试吧 :) 带HTML输出的小演示:结果

r <- Report$new()
r$add(section("Demo"))
r$add(ascii(gen, style=cbind('h', style)))
options(asciiType = "pandoc")
r$backend <- "pandoc"
r$format <- "html"
r$create()
Run Code Online (Sandbox Code Playgroud)

odt输出:结果

r$format <- "odt"
r$create()
Run Code Online (Sandbox Code Playgroud)