我正在尝试创建一个数据表,其单元格根据单元格中的值具有不同的颜色。我可以使用函数 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,并按上述方式运行。安
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".
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)
在这个小练习之后,我倾向于同意 @Drew Steen 的观点,即也可以调查 LaTeX 替代品。例如,检查 here和 here .
关于r - 表格中单元格的条件着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18663159/