表格中单元格的条件着色

表格中单元格的条件着色

本文介绍了表格中单元格的条件着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个数据表,其单元格根据单元格中的值具有不同的颜色.我可以使用 plotrix 包中的函数 addtable2plot 来实现这一点.addtable2plot 函数在已经存在的绘图上放置一个表格.该解决方案的问题是我不想要情节,只想要表格.

I am trying to create a data table whose cells are different colors based on the value in the cell. I can achieve this with the function addtable2plot from the plotrix package. The addtable2plot function lays a table on an already existing plot. The problem with that solution is that I don't want a plot, just the table.

我还查看了 heatmap 函数.问题是我的表中的一些值是字符,而 heatmap 函数,据我所知,只接受数字矩阵.另外,我希望我的列名位于表格的顶部,而不是底部,这似乎不是一个选项.

I've also looked at the heatmap functions. The problem there is that some of the values in my table are character, and the heatmap functions, from what I can tell, only accept numeric matrices. Also, I want my column names to be at the top of the table, not the bottom, and that doesn't seem to be an option.

这是 addtable2plot 的示例代码.如果我能得到一张桌子,填满整个屏幕,那就太好了.

Here's the example code for addtable2plot. If I could get just the table, filling the whole screen, that would be great.

library(plotrix)

testdf<-data.frame(Before=c(10,7,5,9),During=c(8,6,2,5),After=c(5,3,4,3))
rownames(testdf)<-c("Red","Green","Blue","Lightblue")
barp(testdf,main="Test addtable2plot",ylab="Value",
     names.arg=colnames(testdf),col=2:5)
# show most of the options including the christmas tree colors
abg<-matrix(c(2,3,5,6,7,8),nrow=4,ncol=3)
addtable2plot(2,8,testdf,bty="o",display.rownames=TRUE,hlines=TRUE,
              vlines=TRUE,title="The table",bg=abg)

任何帮助将不胜感激.

推荐答案

一个 heatmap 替代方案:

library(gplots)

# need data as matrix
mm <- as.matrix(testdf, ncol = 3)

heatmap.2(x = mm, Rowv = FALSE, Colv = FALSE, dendrogram = "none",
          cellnote = mm, notecol = "black", notecex = 2,
          trace = "none", key = FALSE, margins = c(7, 11))

heatmap.2 中,要绘制 axis 的图的一侧是硬编码的.但是如果你在控制台输入heatmap.2"并将输出复制到编辑器,你可以搜索axis(1,其中1是side 参数(两次命中).然后您可以将 1(图下方的轴)更改为 3(图上方的轴).将更新后的函数分配给新名称,例如 heatmap.3,然后运行它如上.

In heatmap.2 the side of the plot the axis is to be drawn on is hard-coded. But if you type "heatmap.2" at the console and copy the output to an editor, you can search for axis(1, where the 1 is the side argument (two hits). You can then change from a 1 (axis below plot) to a 3 (axis above the plot). Assign the updated function to a new name, e.g. heatmap.3, and run it as above.

library(plotrix)

# while plotrix is loaded anyway:
# set colors with color.scale
# need data as matrix*
mm <- as.matrix(testdf, ncol = 3)
cols <- color.scale(mm, extremes = c("red", "yellow"))

par(mar = c(0.5, 1, 2, 0.5))
# create empty plot
plot(1:10, axes = FALSE, xlab = "", ylab = "", type = "n")

# add table
addtable2plot(x = 1, y = 1, table = testdf,
              bty = "o", display.rownames = TRUE,
              hlines = TRUE, vlines = TRUE,
              bg = cols,
              xjust = 2, yjust = 1, cex = 3)

# *According to `?color.scale`, `x` can be a data frame.
# However, when I tried with `testdf`, I got "Error in `[.data.frame`(x, segindex) : undefined columns selected".
library(plotrix)
par(mar = c(0.5, 8, 3.5, 0.5))
color2D.matplot(testdf,
                show.values = TRUE,
                axes = FALSE,
                xlab = "",
                ylab = "",
                vcex = 2,
                vcol = "black",
                extremes = c("red", "yellow"))
axis(3, at = seq_len(ncol(testdf)) - 0.5,
     labels = names(testdf), tick = FALSE, cex.axis = 2)
axis(2, at = seq_len(nrow(testdf)) -0.5,
     labels = rev(rownames(testdf)), tick = FALSE, las = 1, cex.axis = 2)

经过这个小练习,我倾向于同意@Drew Steen 的观点,即也可以调查 LaTeX 替代品.例如,检查这里此处.

After this little exercise, I tend to agree with @Drew Steen that LaTeX alternatives may be investigated as well. For example, check here and here.

这篇关于表格中单元格的条件着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:44