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

发布于 2024-12-11 01:18:16 字数 763 浏览 1 评论 0原文

是否可以执行以下操作:

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)

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

0 to 0.3   is "red" text color 
0.31 to 0.7 is "yellow"
> 0.7  is "red" 

gen[1,1] 将以红色文本颜色打印“Aa”等等......

感谢您的帮助。

编辑:

我对打印更感兴趣,而不是在图表中绘制。如果我可以将输出保存为 MS Excel 并在 MSEXCEL 中打开,那就太好了。我还可以是其他类型的文本编辑器格式,可以读取颜色编码的文本。因为我的原始数据矩阵的尺寸应该为 1000 x 1000 甚至更大。我想快速了解每个世代类别的真实 p 值。

is it possible to do the following:

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)

I want to print to a file to gen dataframe such way that is conditionally formatted by the pval dataframe. Means than (row1, col1) of gen color depends upon pvale (row1, col1). The following are color coding:

0 to 0.3   is "red" text color 
0.31 to 0.7 is "yellow"
> 0.7  is "red" 

gen[1,1] will be "Aa" printed in red text color and so on....

appreciated your help.

EDITS:

I am more interested in printing not plotting in graph. If I can save output as MS excel and open in MSEXCEL it would be great. I can also be other types of text editors format that can read color coded text. As my orginal data matrix should be of a dimension of 1000 x 1000 or even more. I would like to quicky know unlying p-value for each gen categories.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖哒 2024-12-18 01:18:16

听起来你想模仿 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))

在此处输入图像描述

# 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))

在此处输入图像描述

Sounds like you want to mimic Excel. Here are a couple examples:

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))

enter image description here

# 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))

enter image description here

深居我梦 2024-12-18 01:18:16

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

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

library(xlsx)

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

wb <- createWorkbook()
sheet <- createSheet(wb, "demo")

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

red <- createCellStyle(wb, fillBackgroundColor="tomato", fillForegroundColor="yellow", fillPattern="BIG_SPOTS")
yellow <- createCellStyle(wb, fillBackgroundColor="yellow", fillForegroundColor="tomato", fillPattern="BRICKS1")

以及丑陋的循环使用适当的格式将每个单元格粘贴到电子表格中:

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)
        }
    }
}

保存 Excel 文件:

saveWorkbook(wb, '/tmp/demo.xls')

结果: demo.xls


使用 ascii 包的替代解决方案:

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

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

设置所需的输出:

options(asciiType = "pandoc")

并导出数据框:

> 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**    
--- ---------- ---------- ----------

使用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()

odt 输出:结果

r$format <- "odt"
r$create()

Giving a POC-like answer which is using an ugly loop and not the most beatiful design:

Loading eg. the xlxs package to be able to write to Excel 2007 format:

library(xlsx)

Let us create a workbook and a sheet (see the manual!):

wb <- createWorkbook()
sheet <- createSheet(wb, "demo")

Define some styles to use in the spreadsheet:

red <- createCellStyle(wb, fillBackgroundColor="tomato", fillForegroundColor="yellow", fillPattern="BIG_SPOTS")
yellow <- createCellStyle(wb, fillBackgroundColor="yellow", fillForegroundColor="tomato", fillPattern="BRICKS1")

And the ugly loop which is pasting each cell to the spreadsheet with appropriate format:

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)
        }
    }
}

Saving the Excel file:

saveWorkbook(wb, '/tmp/demo.xls')

Result: demo.xls


Alternative solution with package ascii:

ascii.data.frame() can export data frames to a bunch of formats with the ability of adding some formatting. E.g. exporting to pandoc, first define the styles of each cells to an array with the same dimensions as pval:

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

Set the desired output:

options(asciiType = "pandoc")

And export the data frame:

> 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**    
--- ---------- ---------- ----------

With ascii::Report you could easily convert it it pdf, odt or html. Just try it :) Small demo with HTML output: result

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()

And odt output: result

r$format <- "odt"
r$create()
绅刃 2024-12-18 01:18:16

如果您真的想要这样做(请参阅@Joris 的评论以获得更好的方法),我强烈建议放弃 Excel 并在 LaTeX 中尝试。将 R 包 xtableLaTeX 包结合使用<代码>\colortbl

优点:

  • 打印漂亮
  • 没有 Excel 麻烦(导出到 Excel 很容易;在维护格式的同时导出到 Excel 则困难得多,并且容易出错)

缺点:

  • 它不是 Excel
  • 可能需要一些工作才能使颜色与 xtable。然而,您只需要执行一次,然后它将永远有效——您甚至可以在包中发布您的函数或将其提交给 xtable 维护者以包含在他们的包中,从而省去其他人的麻烦。

If you really want to do this (see @Joris's comment for a better way), I would strongly recommend ditching Excel and trying it in LaTeX. Use the R package xtable combined with the LaTeX package \colortbl.

Advantages:

  • Pretty printing
  • No Excel hassles (exporting to Excel is easy; exporting to Excel while maintaining formatting is much harder, and a recipe for errors)

Disadvantages:

  • It's not Excel
  • It'll probably take some work to make the colors work with xtable. However, you only have to do this once and then it will work forever--you can even release your function in a package or submit it to the xtable maintainers for inclusion in their package and save everyone else the trouble.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文