首页 > 解决方案 > 在同一张纸上安排 ggplot 时出现错误,包括 grobs

问题描述

我在使用以下语法时遇到了问题,我认为这可能是因为我使用的是分面图。

g1<-ggplot(tfp) +
 aes(x = `año`, y = TFP_cd) +
 geom_line(size = 1.25, colour = "#cb181d") +
 labs(x = "Years", y = TeX("$\\ln(TFP)$"), title = "Cobb Douglas TFP Evolution", subtitle = "Mean TFP") +
 theme_light()+theme(plot.title = element_text(face="bold", size=18),
                        plot.subtitle = element_text(size = 15))+scale_x_continuous(breaks = breaks_extended(n=8))

g2 <- ggplot(tfpr) +
 aes(x = `año`, y = TFP_cd, fill = rama, colour = rama) +
 geom_line(size = 1.25) +
 scale_fill_brewer(palette = "Set1") +
 scale_color_brewer(palette = "Set1") +
 labs(x = "Year", y =TeX("$\\ln(TFP)$"), title = "Cobb Douglas TFP Evolution", subtitle = "On ISIC 1 level") +
 theme_light() +
 theme(legend.position = "none") +
 facet_wrap(~(rama), scales = "free")+scale_x_continuous(breaks = breaks_extended(n=8))+
  theme(plot.title = element_text(face="bold", size=18),plot.subtitle = element_text(size = 15))+
  theme(strip.text.x = element_text(size = 12, color = "black", face = "bold"))

g3 <- ggplot(tfpv) +
 aes(x = año, y = TFP_cd, colour = size) +
 geom_line(size = 1.25) +
 scale_color_brewer(palette = "Set1") +
 labs(x = "Year", y =TeX("$\\ln(TFP)$"), title = "Cobb Douglas TFP Evolution", subtitle = "On Andean Community Sale Size Classification") +
 theme_light()+scale_x_continuous(breaks = breaks_extended(n=8))+
  theme(plot.title = element_text(face="bold", size=18),plot.subtitle = element_text(size = 14))+
  theme(strip.text.x = element_text(size = 12, color = "black", face = "bold"))

所以我有包library(ggpubr),我一直这样做:

ggarrange(g1,g2,g3,
          ncols=1, nrows=3)

但它给了我这个错误:

> ggarrange(g1,g2,g3,
+           ncols=1, nrows=3)
Warning messages:
1: In as_grob.default(plot) :
  Cannot convert object of class numeric into a grob.
2: In as_grob.default(plot) :
  Cannot convert object of class numeric into a grob.

还有这张图。我只需要该图,但需要 1 列 3 行。(垂直的) 在此处输入图像描述

提前致谢。

标签: rggplot2plotggpubr

解决方案


推荐阅读