2017-05-23 15 views
0

Rの出力用テーブルの書式設定方法を学習しようとしています。今ではテーブルをexampleにして列の数値を書式設定しようとしています。いくつかの列には2桁、ランク列のようないくつかの列はありません。また、行の名前をYear Monthとして保持したいと思います。このようなテーブルを作成するにはどうすればいいですか?テーブルの列で数値を書式設定する方法(tableGrob)

注:「sepal.with」列の「3」も「3.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) 
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) 

答えて

0

機能的で柔軟性がありますが、私はMattのformatが少し複雑に機能することを発見しました。少しsprintfmagicで十分です。また、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ビット「賢く」製)

enter image description here

+0

私はマッチさせるために "apply(iris [、-4]、2、zeropad)"という行を変更しなければならないと信じています。(zeropad、iris [、-4]、2) – user2946746

+0

@ user2946746: 2つのゼロを埋めたい場合、 'apply(iris [、-4]、2、zeropad、2)'、 'mapply(zeropad、iris [、-4]、2)'または 'sapply(iris [、-4]、ゼロパック、2) '。彼らはすべて同じ結果を与えるでしょう。配列や行列には 'apply'を使い、オブジェクトのようなリスト(データフレーム)には' * apply'(inc。 'mapply'と' sapply')を使う傾向があります。 – AkselA

+0

ありがとうございました。私は、それぞれの列を別々にフォーマットしようとしました。 iris [、c(1)] < - mapply(ゼロパッド、アイリス[、c(1)]、0)。私は列の色分けによって列を失う。どんな考え? – user2946746

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) 
+0

私はチャートに気づいたそれは色の書式だ失います。彼らはもはや列ではなく、列によって。 – user2946746

関連する問題