多个绘图图片ggplot和Gridextra

时间:2018-09-10 14:28:26

标签: r ggplot2 gridextra

我正在尝试使用gridExtra包在彼此之上创建三个ggplot图的图片。

我正在使用的代码是这样:

require(ggplot2)
library(gridExtra)
library(scales)
library(ggpubr)
library(tidyr)
library(reshape)
library(dplyr)

## GRAPHS

# yearly funding required
yearly1 <- ggplot(yearly, aes(x=year, y=Total_US_required/1000000)) + 
  ggtitle("Total Funding Required") + 
  geom_line(color="darkblue") + 
  labs(y="In Million USD", x="") + xlim(2006,2017)

# yearly funding received
yearly2 <- ggplot(yearly, aes(x=year, y=Total_US_received/1000000)) + 
  ggtitle("Total Funding Received") + 
  geom_line(color="darkblue") + 
  labs(y="", x="") + xlim(2006,2017)

# Yealy funding CERF
yearly3 <- ggplot(yearly, aes(x=year, y=Total_US_received_from.CERF/1000000)) + 
  ggtitle("Total Funding Received CERF") + 
  geom_line(color="darkblue") + 
  labs(y="", x="") + xlim(2006,2017)

## COMBINED PICTURES

tiff('fig1a.tiff', units="in", width=5, height=8.3, res=300)        
grid.arrange(yearly1, yearly2, yearly3, nrow=3, 
             left = textGrob("In Million USD", rot = 90, vjust = 1, hjust=0, 
                             gp = gpar(fontsize = 10)))
dev.off()

我收到以下错误消息:

Error in grid::textGrob(label = label, just = just, hjust = hjust, vjust = vjust,  : 
formal argument "gp" matched by multiple actual arguments

有关如何创建此图片的任何建议?越容易越好。

这是我的数据(压缩后):

structure(list(year = 2006:2010, Total_US_received_from.CERF = c(65091455.5, 
    204409211, 200351170.5, 180924822, 290493687.5), Total_US_required = c(828538873.5, 
    2986369814, 1554939106, 2144420061, 7724926517), Total_US_received = c(215790327.5, 
    1988581112, 1698825389, 1509823414, 4975642034)), row.names = c(NA, 
    5L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

使用ggarrange中的ggpubr

yearly1 <- ggplot(yearly, aes(x=year, y=Total_US_required/1000000)) + ggtitle("Total Funding Required") + geom_line(color="darkblue") + labs(y="In Million USD", x="") + xlim(2006,2017)

yearly2 <- ggplot(yearly, aes(x=year, y=Total_US_received/1000000)) + ggtitle("Total Funding Received") + geom_line(color="darkblue") + labs(y="In Million USD", x="") + xlim(2006,2017)

yearly3 <- ggplot(yearly, aes(x=year, y=Total_US_received_from.CERF/1000000)) + ggtitle("Total Funding Received CERF") + geom_line(color="darkblue") + labs(y="In Million USD", x="") + xlim(2006,2017)

ggarrange(yearly1, yearly2, yearly3, 
          ncol = 1, nrow = 3)

enter image description here