如何在表格中按列格式化数字(tableGrob)

时间:2017-05-23 17:08:19

标签: r

我一直在努力学习如何在R中为输出格式化表格。我现在试图在example中取表并格式化列中的数字。我想在某些列中有两个数字,在某些列中没有,如rank列。另外,我想将行名称保留为Year Year。怎么会创建像这样的表?

注意:" 3"在专栏#sepal.with'印刷为" 3"不是" 3.0"。

library(gtable)
library(grid)
library(gridExtra)
library(zoo)

iris <- as.matrix(iris[1:4, 1:3])
rownames(iris)<-as.yearmon(seq(as.Date("2000/1/1"), as.Date("2000/4/1"), by = "month"))
RankColumn<-seq(1, 4, by = 1)
iris<-cbind(iris, RankColumn)
iris<- round(as.matrix(iris), digits=2)


# a simple function to scale each column to the range [0, 1]
norm <- function(x) {
apply(x, 2, function(y){(y-min(y))/(max(y)-min(y))})
}

bluecol <- colorRamp(c("#3366EE", "#AABBFF", "#DDDDFF"))(norm(iris))
bluecol <- rgb(bluecol[, 1], bluecol[, 2], bluecol[, 3], max=255)

tt <- ttheme_default(core=list(bg_params=list(fill=bluecol)))

g <- tableGrob(iris, theme=tt)
g <- gtable_add_grob(g,
grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
t = 2, b = nrow(g), l = 1, r = ncol(g))
g <- gtable_add_grob(g,
grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
t = 1, l = 1, r = ncol(g))
grid.draw(g)

2 个答案:

答案 0 :(得分:3)

这样的事情可能会让你获得你正在寻找的格式。

library(gtable)
library(grid)
library(gridExtra)
library(zoo)

iris <- as.matrix(iris[1:4, 1:3])
rownames(iris)<-as.yearmon(seq(as.Date("2000/1/1"), as.Date("2000/4/1"), by = "month"))
RankColumn<-seq(1, 4, by = 1)
iris<-cbind(iris, RankColumn)

# Create the matrix
iris<- as.matrix(iris)


# a simple function to scale each column to the range [0, 1]
norm <- function(x) {
  apply(x, 2, function(y){(y-min(y))/(max(y)-min(y))})
}

# function to format columns
format.column <- function(matrix, colnum, rounding, decimals){
  formatted <- format(round(as.numeric(matrix[,colnum]), digits = rounding),nsmall = decimals)
  return(formatted)
}

bluecol <- colorRamp(c("#3366EE", "#AABBFF", "#DDDDFF"))(norm(iris))
bluecol <- rgb(bluecol[, 1], bluecol[, 2], bluecol[, 3], max=255)

tt <- ttheme_default(core=list(bg_params=list(fill=bluecol)))

# Set formatting for individual columns 
# Adjust these or add additional columns to format as necessary
iris[,1] <- format.column(matrix = iris, colnum = 1, rounding = 2, decimals = 2)
iris[,2] <- format.column(matrix = iris, colnum = 2, rounding = 2, decimals = 1)

g <- tableGrob(iris, theme=tt)
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 2, b = nrow(g), l = 1, r = ncol(g))
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 1, l = 1, r = ncol(g))
grid.draw(g)

答案 1 :(得分:0)

虽然功能性和灵活性,但我发现Matt的format功能有点过于复杂。一点sprintf magic就足够了。此外,将yearmon对象转换为字符将保留格式。

library(gtable)
library(grid)
library(gridExtra)
library(zoo)

data(iris)
iris <- iris[1:4, 1:3]
rownames(iris) <- as.character(as.yearmon(
  seq(as.Date("2000/1/1"), as.Date("2000/4/1"), by = "month")))
iris$RankColumn <- 1:nrow(iris)

# a simple function to scale each row or column to the range [0, 1]
# will convert characters to numerics if in a sensible format
norm <- function(x, mar=2) {
    rnames <- rownames(x)
    x <- apply(x, 2, as.numeric)
    x <- apply(x, mar, function(y){(y-min(y))/(max(y)-min(y))})
    rownames(x) <- rnames
    x
}

# function to pad with zero
# by default does not pad integers
zeropad <- function(x, nz=1, exc.int=TRUE) {
    if (is.integer(x) & exc.int) {
        x
    } else { 
        sprintf(paste0("%.", nz, "f"), x)
    }
}

bluecol <- colorRamp(c("#3366EE", "#AABBFF", "#DDDDFF"))(norm(iris))
bluecol <- rgb(bluecol[, 1], bluecol[, 2], bluecol[, 3], max=255)

tt <- ttheme_default(core=list(bg_params=list(fill=bluecol)))

# convert floats to zero-padded characters
iris[1:ncol(iris)] <- sapply(iris, zeropad, 2)

g <- tableGrob(iris, theme=tt)
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 2, b = nrow(g), l = 1, r = ncol(g))
g <- gtable_add_grob(g,
                     grobs = rectGrob(gp = gpar(fill = NA, lwd = 2)),
                     t = 1, l = 1, r = ncol(g))
plot.new()
grid.draw(g)

(已编辑为专门使用数据框。normzeropad做得更聪明&#39;)

enter image description here