Reputation: 1780
I have been trying to learn how to format tables for output in R. I'm now trying to take the table in example and format the numbers in the columns. I would like to have two digits in some columns and none in some columns like the rank column. Also, I would like to keep the row names as Year Month. How would one go about creating a table like this?
Note: Also the "3" in the column 'sepal.with' is printed as "3" not "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)
Upvotes: 0
Views: 3448
Reputation: 8836
While functional and flexible, I found Matt's format
function a bit over complicated. A little sprintf
magic should suffice. Also, converting the yearmon
objects to character will keep the format.
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)
(edited to work exclusively with data frames. norm
and zeropad
made a bit 'smarter')
Upvotes: 0
Reputation: 3369
Something like this may work to get you the format you're looking for.
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)
Upvotes: 3