将子文本添加到视口

问题描述 投票:1回答:3

我想在textGrob grid上添加子文本(Viewport) - 这可能吗?

我已经想出如何将子文本添加到单个图表:

require(ggplot2) 
require(gridExtra)

# make the data
timeSeries <- data.frame(date = seq(as.Date("2001-01-01"), as.Date("2012-01-01"), by = "mon"), 
                     value = 1:133 + rnorm(133, 0, 10)
                     )


# make the ggplots
gpLine <- ggplot(timeSeries, aes(x = date, y = value)) +
                geom_line()

gpBar <- ggplot(timeSeries, aes(x = date, y = value)) +
                geom_bar(stat = 'identity')

# ggplot + subtext
grid.arrange(gpBar, sub = textGrob("why is this at the bottom?"))

...我可以使用grid Viewport将两张图表粘在一起

# two plots, one view
vplayout <- function(x,y) viewport(layout.pos.row = x, layout.pos.col = y)

pushViewport(viewport(layout = grid.layout(3,1)))
print(gpLine, vp = vplayout(1:2, 1))
print(gpBar, vp = vplayout(3, 1))

...但我无法弄清楚如何将文本添加到结果视口的底部。

Grid是如此完整,我敢肯定必须有办法,但它对我来说是隐藏的。

r ggplot2 r-grid
3个回答
3
投票

你非常接近:

popViewport() # brings you to the top viewport for the whole plotting area
grid.text("Hello Vicar", x = unit(0.5, "npc"), y = unit(0.25, "npc"))

完成上述所有命令之后。


3
投票

也许我错过了一些东西,因为我会简单地做以下事情,

grid.arrange(gpLine, gpBar, heights = c(2/3, 1/3), 
     bottom = textGrob("this is my signature", x=1, hjust=1, vjust=0))

编辑(2015年7月23日):从v> = 2.0.0的gridExtra,参数已从sub更改为bottom以保持一致性


0
投票

在当前版本的ggplot2中,不需要使用gridExtra::grid.arrange

p <- ggplot(...) + labs(caption = 'text at the bottom')

诀窍。

© www.soinside.com 2019 - 2024. All rights reserved.