0

By running the following code, I can receive the saved PNG in the file location. According to the modified time, they are generated by this execution of code. When I try to merge this two PNG into one plot, I found the error message:

 multiplot(plot1, plot2, cols = 2)
 Error: Aesthetics must be either length 1 or the same as the data (14): x, y 

And I can't load the plot1 and plot2 in the plot section in RStudio.

dat <- fread('IG_post.csv')
plot1 <- ggplot(dat, aes(x = dat$Parameter, y = dat$Mean)) +
  geom_point(size = 2) +
  geom_errorbar(width = 0.1, aes(ymax = dat$`0.975`, ymin = dat$`0.025`)) +
  geom_hline(yintercept = 0, col = 'blue') +
  labs(x='beta', y='value', title = 'Zero-inflated Gamma Model') +
  theme_bw()+ theme(axis.text.x=element_text(angle=45, hjust=1), plot.title = element_text(hjust = 0.5))

file_name <- paste0('C:/Users/Qiangsuper/Dropbox/Papers/1/plot/IG_post.png')
png(filename = file_name, width = 1200, height = 600, res = 240)
plot1
dev.off()


dat <- fread('Tweedie_post.csv')
plot2 <- ggplot(dat, aes(x = dat$Parameter, y = dat$Mean)) +
  geom_point(size = 2) +
  geom_errorbar(width = 0.1, aes(ymax = dat$`0.975`, ymin = dat$`0.025`)) +
  geom_hline(yintercept = 0, col = 'blue') +
  labs(x='beta', y='value', title = 'Tweedie Model') +
  theme_bw()+ theme(axis.text.x=element_text(angle=45, hjust=1), plot.title = element_text(hjust = 0.5))

file_name <- paste0('C:/Users/Qiangsuper/Dropbox/Papers/1/plot/Tweedie_post.png')
png(filename = file_name, width = 1200, height = 600, res = 240)
plot2
dev.off()


file_name <- paste0('C:/Users/Qiangsuper/Dropbox/Papers/1/plot/post.png')
png(filename = file_name, width = 1200, height = 600, res = 240)
multiplot(plot1, plot2, cols = 2)
dev.off()

Please help me to figure out where I went wrong. Thank you very much for your help.

Qiang Super
  • 323
  • 1
  • 11
  • Which package is `multiplot` from? – Z.Lin Apr 26 '19 at 05:40
  • @qiang-super not sure about the error, but you can use `grid.arrange()` from the library `gridExtra` like, `library("gridExtra") grid.arrange(plot1, plot2)` – mnm Apr 26 '19 at 05:44
  • @Qiang Super: try this https://stackoverflow.com/a/48164920/786542 – Tung Apr 26 '19 at 07:41
  • Thanks, @Z.Lin. The multiplot is a self-defined function to combine multi-plots into one. – Qiang Super Apr 26 '19 at 14:13
  • Thanks, @mnm. I am not sure grid.arrange can be used in the ggplots. I don't think the problem comes from the multiplot command, since I can't even display plot1 and plot2. – Qiang Super Apr 26 '19 at 14:16
  • Thanks, @Tung. I am not sure this problem comes from the multiplot command, since I can't display plot1 and plot2. – Qiang Super Apr 26 '19 at 14:17
  • @QiangSuper I don't think we can help you to figure out where you went wrong if we have sight of neither your self-defined function nor your data. – Z.Lin Apr 26 '19 at 14:25

0 Answers0