3

我正在尝试制作这样的图形:

在此处输入图像描述

生存/回归分析结果的最佳/有效绘图

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)

在此处输入图像描述

是否可以使用ggplotgtable更改 ggplot 面板的背景颜色并将 Zebra 样式设置为它,就像其他两个 gtable 一样类似于第一个示例?

谢谢,

4

1 回答 1

2

正如@MrFlick所说,您必须在背景中添加某种几何图层。这是我的方法:
创建两个数据框,一个用于点,一个用于段,并调整段大小以匹配表行。

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)

在此处输入图像描述

于 2017-09-20T15:24:46.193 回答