r - 表格中单元格的条件着色

标签 r plot heatmap

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

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

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

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)

任何帮助将不胜感激。

最佳答案

A 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,并按上述方式运行。

enter image description here

addtable2plot替代
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".

enter image description here

A color2D.matplot替代
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)

enter image description here

在这个小练习之后,我倾向于同意 @Drew Steen 的观点,即也可以调查 LaTeX 替代品。例如,检查 herehere .

关于r - 表格中单元格的条件着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18663159/

相关文章:

python - 如何在 seaborn 热图标签中使用科学记数法?

r - 使用 R 中的 str_count 函数计算多个模式

r - 在 dplyr 中使用 group_by() 与 filter() 类似吗?

r - 在R回归中获得预测变量名称

r - 如何使用 colnames 作为循环的主标题

matlab - 为什么 matlab plot 使用这么多内存?

python - 在 matplotlib 中绘制 10 负幂的 xscale

r - 如何从 ggplot2 的热图中排除异常值?

r - 在 Rcpp 中提取矩阵行

python - 来自枢轴的 seaborn 热图中的数据顺序