我尝试创建Infographisc报告,但我不能在此报告中使用多个图表。
dat <- get.ohlc.yahoo('AAPL', '2015-12-01', '2015-12-31')
dat1 <- get.ohlc.yahoo('GOOG', '2015-12-01', '2015-12-31')
p1 <- ggplot(dat, aes(date, adjusted)) + geom_line() + theme_minimal() +
theme(axis.title.x = element_blank(), axis.text.x = element_blank())
p2 <- ggplot(dat,aes(date, volume)) + geom_bar(stat="identity") + theme_minimal() +
theme(axis.title.x = element_blank(),axis.text.x = element_text(angle=90))
p3 <- ggplot(dat1, aes(date, adjusted)) + geom_line() + theme_minimal() +
theme(axis.title.x = element_blank(), axis.text.x = element_blank())
p4 <- ggplot(dat1,aes(date, volume)) + geom_bar(stat="identity") + theme_minimal() +
theme(axis.title.x = element_blank(),axis.text.x = element_text(angle=90))
当我使用时:
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
pushViewport(viewport(layout = grid.layout(4, 3)))
pdf("plot.pdf")
grid::grid.newpage()
pushViewport(viewport(layout = grid.layout(4, 1)))
print(p1, vp=vplayout(1,1))
print(p2, vp=vplayout(2,1))
print(p3, vp=vplayout(3,1))
print(p4, vp=vplayout(4,1))
dev.off()
代码工作得很好,但是当我不在报告图上创建多个图时,看起来不像以前那样好:
pdf("plot.pdf")
grid::grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 1)))
grid.arrange(p1, p2, ncol = 1, heights = c(2, 1))
grid.arrange(p3, p4, ncol = 1, heights = c(2, 1))
dev.off()