像在 gtable 中一样制作 ggplot 面板背景
make ggplot panel-background like in gtable
我正在尝试制作这样的图形:
以上是我的示例代码:
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 面板的背景颜色并像其他两个 gtables 一样将 Zebra 样式设置为类似于第一个示例? .
谢谢,
正如@MrFlick所说,您必须在背景中添加某种几何图层。这是我的方法:
创建两个数据框,一个用于点,一个用于段,并调整段大小以匹配 table 行。
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)
我正在尝试制作这样的图形:
以上是我的示例代码:
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 面板的背景颜色并像其他两个 gtables 一样将 Zebra 样式设置为类似于第一个示例? .
谢谢,
正如@MrFlick所说,您必须在背景中添加某种几何图层。这是我的方法:
创建两个数据框,一个用于点,一个用于段,并调整段大小以匹配 table 行。
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)