R 表中单元格的条件着色

R 表中单元格的条件着色,r,plot,heatmap,R,Plot,Heatmap,我正在尝试创建一个数据表,其单元格根据单元格中的值具有不同的颜色。我可以通过plotrix软件包中的函数addtable2plot实现这一点。addtable2plot函数将表格放置在已存在的绘图上。这个解决方案的问题是,我不需要绘图,只需要表格 我还研究了热图函数。这里的问题是,我的表中的一些值是字符,heatmap函数,据我所知,只接受数字矩阵。此外,我希望我的列名位于表的顶部,而不是底部,这似乎不是一个选项 下面是addtable2plot的示例代码。如果我能得到一张桌子,填满整个屏幕,那

我正在尝试创建一个数据表,其单元格根据单元格中的值具有不同的颜色。我可以通过
plotrix
软件包中的函数
addtable2plot
实现这一点。
addtable2plot
函数将表格放置在已存在的绘图上。这个解决方案的问题是,我不需要绘图,只需要表格

我还研究了
热图
函数。这里的问题是,我的表中的一些值是字符,
heatmap
函数,据我所知,只接受数字矩阵。此外,我希望我的列名位于表的顶部,而不是底部,这似乎不是一个选项

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

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

testdf是一种基于
ggplot2
的黑客解决方案。我不完全理解您实际上希望如何映射颜色,因为在您的示例中,表中的颜色没有映射到
testdf
的行名,但在这里,我已将颜色映射到
(转换为因子)


testdf$color一种基于
ggplot2
的黑客解决方案。我不完全理解您实际上希望如何映射颜色,因为在您的示例中,表中的颜色没有映射到
testdf
的行名,但在这里,我已将颜色映射到
(转换为因子)

testdf$colorA
heatmap
alternative:


A
color2D.matplot
alternative

经过这个小小的练习后,我倾向于同意@Drew Steen的观点,也可以研究乳胶替代品。例如,检查并选择。

A
热图
备选方案:


A
color2D.matplot
alternative


经过这个小小的练习后,我倾向于同意@Drew Steen的观点,也可以研究乳胶替代品。例如,检查并。

您可以使用grid和gtable攻击某些东西

palette(c(RColorBrewer::brewer.pal(8, "Pastel1"),
          RColorBrewer::brewer.pal(8, "Pastel2")))


library(gtable)
gtable_add_grobs <- gtable_add_grob # alias

d <- head(iris, 3)
nc <- ncol(d)
nr <- nrow(d)

extended_matrix <- cbind(c("", rownames(d)), rbind(colnames(d), as.matrix(d))) 

## text for each cell
all_grobs <- matrix(lapply(extended_matrix, textGrob), ncol=ncol(d) + 1)

## define the fill background of cells
fill <- lapply(seq_len(nc*nr), function(ii) 
  rectGrob(gp=gpar(fill=ii)))

## some calculations of cell sizes
row_heights <- function(m){
  do.call(unit.c, apply(m, 1, function(l)
    max(do.call(unit.c, lapply(l, grobHeight)))))
}

col_widths <- function(m){
  do.call(unit.c, apply(m, 2, function(l)
    max(do.call(unit.c, lapply(l, grobWidth)))))
}

## place labels in a gtable
g <- gtable_matrix("table", grobs=all_grobs, 
                   widths=col_widths(all_grobs) + unit(4,"mm"), 
                   heights=row_heights(all_grobs) + unit(4,"mm"))

## add the background
g <- gtable_add_grobs(g, fill, t=rep(seq(2, nr+1), each=nc), 
                      l=rep(seq(2, nc+1), nr), z=0,name="fill")

## draw
grid.newpage()
grid.draw(g)
调色板(c(RColorBrewer::brewer.pal(8,“Pastel1”), RColorBrewer::brewer.pal(8,“Pastel2”)) 图书馆(gtable)
gtable_add_grobs你可以用grid和gtable攻击一些东西

palette(c(RColorBrewer::brewer.pal(8, "Pastel1"),
          RColorBrewer::brewer.pal(8, "Pastel2")))


library(gtable)
gtable_add_grobs <- gtable_add_grob # alias

d <- head(iris, 3)
nc <- ncol(d)
nr <- nrow(d)

extended_matrix <- cbind(c("", rownames(d)), rbind(colnames(d), as.matrix(d))) 

## text for each cell
all_grobs <- matrix(lapply(extended_matrix, textGrob), ncol=ncol(d) + 1)

## define the fill background of cells
fill <- lapply(seq_len(nc*nr), function(ii) 
  rectGrob(gp=gpar(fill=ii)))

## some calculations of cell sizes
row_heights <- function(m){
  do.call(unit.c, apply(m, 1, function(l)
    max(do.call(unit.c, lapply(l, grobHeight)))))
}

col_widths <- function(m){
  do.call(unit.c, apply(m, 2, function(l)
    max(do.call(unit.c, lapply(l, grobWidth)))))
}

## place labels in a gtable
g <- gtable_matrix("table", grobs=all_grobs, 
                   widths=col_widths(all_grobs) + unit(4,"mm"), 
                   heights=row_heights(all_grobs) + unit(4,"mm"))

## add the background
g <- gtable_add_grobs(g, fill, t=rep(seq(2, nr+1), each=nc), 
                      l=rep(seq(2, nc+1), nr), z=0,name="fill")

## draw
grid.newpage()
grid.draw(g)
调色板(c(RColorBrewer::brewer.pal(8,“Pastel1”), RColorBrewer::brewer.pal(8,“Pastel2”)) 图书馆(gtable)
gtable_add_grobs您可以从初始化空白绘图开始吗<代码>绘图(0,type=“n”,bty=“n”,xaxt=“n”,yaxt=“n”,ylab=“”,xlab=“”)
相关:您可以从初始化空白绘图开始吗<代码>绘图(0,type=“n”,bty=“n”,xaxt=“n”,yaxt=“n”,ylab=“”,xlab=“”)
相关:
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)
palette(c(RColorBrewer::brewer.pal(8, "Pastel1"),
          RColorBrewer::brewer.pal(8, "Pastel2")))


library(gtable)
gtable_add_grobs <- gtable_add_grob # alias

d <- head(iris, 3)
nc <- ncol(d)
nr <- nrow(d)

extended_matrix <- cbind(c("", rownames(d)), rbind(colnames(d), as.matrix(d))) 

## text for each cell
all_grobs <- matrix(lapply(extended_matrix, textGrob), ncol=ncol(d) + 1)

## define the fill background of cells
fill <- lapply(seq_len(nc*nr), function(ii) 
  rectGrob(gp=gpar(fill=ii)))

## some calculations of cell sizes
row_heights <- function(m){
  do.call(unit.c, apply(m, 1, function(l)
    max(do.call(unit.c, lapply(l, grobHeight)))))
}

col_widths <- function(m){
  do.call(unit.c, apply(m, 2, function(l)
    max(do.call(unit.c, lapply(l, grobWidth)))))
}

## place labels in a gtable
g <- gtable_matrix("table", grobs=all_grobs, 
                   widths=col_widths(all_grobs) + unit(4,"mm"), 
                   heights=row_heights(all_grobs) + unit(4,"mm"))

## add the background
g <- gtable_add_grobs(g, fill, t=rep(seq(2, nr+1), each=nc), 
                      l=rep(seq(2, nc+1), nr), z=0,name="fill")

## draw
grid.newpage()
grid.draw(g)