我正在尝试制作这样的图形:
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)
ggplot
和 gtable
是否可以更改 ggplot 面板的背景颜色并将 Zebra 样式设置为与其他两个 gtables 类似,以便与第一个示例类似?。谢谢,
最佳答案
正如 @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)
关于r - 使 ggplot 面板背景像在 gtable 中一样,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46324520/