条件格式:使单元格丰富多彩
是否可以执行以下操作:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你想模仿 Excel。以下是几个示例:
Sounds like you want to mimic Excel. Here are a couple examples:
给出一个类似 POC 的答案,它使用了一个丑陋的循环,而不是最漂亮的设计:
加载例如。
xlxs
包能够写入 Excel 2007 格式:让我们创建一个工作簿和一个工作表(请参阅手册!):
定义一些要在电子表格中使用的样式:
以及丑陋的循环使用适当的格式将每个单元格粘贴到电子表格中:
保存 Excel 文件:
结果: demo.xls
使用
ascii
包的替代解决方案:ascii.data.frame()
可以将数据帧导出为多种格式添加一些格式的能力。例如导出到pandoc,首先将每个单元格的样式定义为与pval
具有相同维度的数组:设置所需的输出:
并导出数据框:
使用
ascii::Report
您可以轻松地将其转换为 pdf、odt 或 html。尝试一下吧:)带有 HTML 输出的小演示:结果和 odt 输出:结果
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:Let us create a workbook and a sheet (see the manual!):
Define some styles to use in the spreadsheet:
And the ugly loop which is pasting each cell to the spreadsheet with appropriate format:
Saving the Excel file:
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 aspval
:Set the desired output:
And export the data frame:
With
ascii::Report
you could easily convert it it pdf, odt or html. Just try it :) Small demo with HTML output: resultAnd odt output: result
如果您真的想要这样做(请参阅@Joris 的评论以获得更好的方法),我强烈建议放弃 Excel 并在 LaTeX 中尝试。将 R 包
xtable
与 LaTeX 包结合使用<代码>\colortbl。优点:
缺点:
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:
Disadvantages:
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.