2017-09-20 16 views
3

のように:メイクggplotパネル-背景私はこのようなグラフィックを作るしようとしているgtable

enter image description here

Optimal/efficient plotting of survival/regression analysis results

のhierは私のサンプルコードです:

library(gtable) 
library(grid) 
library(gridExtra) 
library(ggplot2) 

tg <- tableGrob(iris[1:5,1:3], rows = NULL, cols=NULL) 
tg$heights <- unit(rep(1,nrow(tg)), "null") 

p <- qplot(1:5,1:5) + ggtitle("Title", subtitle = "another line") + 
theme_grey(12) + 
scale_y_continuous(expand=c(0,0.5)) 
g <- ggplotGrob(p) 
g <- gtable::gtable_add_cols(g, widths = sum(tg$widths), pos = 0) 
g <- gtable::gtable_add_cols(g, widths = sum(tg$widths), pos = -1) 
g <- gtable::gtable_add_grob(g, list(tg, tg), t = 6, l=c(1,ncol(g)), r=c(1,ncol(g))) 
grid.newpage() 
grid.draw(g) 

enter image description here

ggplotgtableでggplotパネルの背景色を変更し、Zebraスタイルを他の2つのgtablesのように設定して、最初の例に似ていますか?

おかげで、@MrFlickとして

+0

。バックグラウンドバンディングの組み込みオプションはありません。 – MrFlick

+0

しかし、どのようにmin-x max-xとmin-y max-yを他のgtablesの長方形と同じ高さに設定することができますか? – Tlopasha

答えて

2

はあなたがバックグラウンドでGEOM層のいくつかの種類を追加する必要が言いました。
2つのデータフレームを作成します.1つはポイント用、もう1つはセグメント用で、セグメントサイズをテーブル行に合わせて調整します。あなたはおそらく、すべて一緒に背景をオフにする(または白に設定)し、あなたのポイントの下の層として灰色の長方形を自分で描きたいと思いggplotで

tg <- tableGrob(iris[1:5,1:3], rows = NULL, cols=NULL) 
tg$heights <- unit(rep(1,nrow(tg)), "null") 
dSegment <- data.frame(X = 0, XE = 6, Y = 1:5, YE = 1:5) 
dPoint <- data.frame(X = 1:5, Y = 1:5) 

p <- ggplot() + 
    geom_segment(data = dSegment, 
       aes(x = X, xend = XE, y = Y, yend = YE, color = factor(Y %% 2)), 
       size = 40) + 
    geom_point(data = dPoint, aes(X, Y)) + 
    ggtitle("Title", subtitle = "another line") + 
    scale_y_continuous(expand = c(0, 0.5)) + 
    scale_x_continuous(breaks = c(1:5)) + 
    scale_color_manual(values = c("#dbdbdb", "#e9e9e9")) + 
    theme_grey(12) + 
    theme(panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(), 
      panel.background = element_blank(), 
      legend.position = "none") 
g <- ggplotGrob(p) 
g <- gtable::gtable_add_cols(g, widths = sum(tg$widths), pos = 0) 
g <- gtable::gtable_add_cols(g, widths = sum(tg$widths), pos = -1) 
g <- gtable::gtable_add_grob(g, list(tg, tg), t = 6, l=c(1,ncol(g)), r=c(1,ncol(g))) 
grid.newpage() 
grid.draw(g) 

enter image description here

+0

あなたのansowerのPoGibasありがとう、はい私thnikこれを行うには、最善の方法です。 – Tlopasha

関連する問題