我想使用ggpubr绘制2个图,但我得到错误:
Error in ggplot_to_gtable(x) :
Argument needs to be of class "ggplot" or "gtable"
我需要使用ggpubr,因为我想要一个共享的图例。
library(ggpubr)
dat =data.frame(x = 1:3, y = 1:3)
g1= ggplot(dat, aes(x =x , y = y))+geom_point()
g2= ggplot(dat, aes(x =x , y = y))+geom_point()
ggarrange(g1,g2, nrow = 1)
任何想法如何解决这个问题?
答案 0 :(得分:1)
请注意,图中实际上没有任何图例。下面我使用color
参数为每个情节创建了一个图例。
library(ggplot2)
library(cowplot)
library(ggpubr)
library(devtools)
# data
dat <- data.frame(x = 1:3, y = 1:3)
g1 <- ggplot2::ggplot(dat, aes(x = x , y = y, color = x)) + geom_point()
g2 <- ggplot2::ggplot(dat, aes(x = x , y = y, color = x)) + geom_point()
# solution with ggpubr (note that I am leaving out nrow here because it doesn't work with that argument,
# so there might be indeed be a bug in this function; you should raise this on GitHub page for ggpubr)
ggpubr::ggarrange(g1, g2, common.legend = TRUE, legend = "right")
# solution with cowplot (where you can specify nrow argument and it works)
prow <- cowplot::plot_grid(
g1 + theme(legend.position = "none"),
g2 + theme(legend.position = "none"),
nrow = 1,
align = 'vh',
labels = c("A", "B"),
hjust = -1
)
legend <- cowplot::get_legend(g1)
cowplot::plot_grid( prow, legend, rel_widths = c(3, .3))
# session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#> setting value
#> version R version 3.4.1 (2017-06-30)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United States.1252
#> tz America/New_York
#> date 2018-02-09
#> Packages -----------------------------------------------------------------
#> package * version date source
#> assertthat 0.2.0 2017-04-11 CRAN (R 3.4.1)
#> backports 1.1.2 2017-12-13 CRAN (R 3.4.1)
#> base * 3.4.1 2017-06-30 local
#> bindr 0.1 2016-11-13 CRAN (R 3.4.1)
#> bindrcpp 0.2 2017-06-17 CRAN (R 3.4.1)
#> colorspace 1.3-2 2016-12-14 CRAN (R 3.4.1)
#> compiler 3.4.1 2017-06-30 local
#> cowplot * 0.9.2 2017-12-17 CRAN (R 3.4.1)
#> datasets * 3.4.1 2017-06-30 local
#> devtools * 1.13.4 2017-11-09 CRAN (R 3.4.3)
#> digest 0.6.15 2018-01-28 CRAN (R 3.4.3)
#> dplyr 0.7.4 2017-09-28 CRAN (R 3.4.2)
#> evaluate 0.10.1 2017-06-24 CRAN (R 3.4.3)
#> ggplot2 * 2.2.1 2016-12-30 CRAN (R 3.4.3)
#> ggpubr * 0.1.6 2017-11-14 CRAN (R 3.4.1)
#> glue 1.2.0.9000 2018-01-05 Github (tidyverse/glue@1592ee1)
#> graphics * 3.4.1 2017-06-30 local
#> grDevices * 3.4.1 2017-06-30 local
#> grid 3.4.1 2017-06-30 local
#> gridExtra 2.3 2017-09-09 CRAN (R 3.4.2)
#> gtable 0.2.0 2016-02-26 CRAN (R 3.4.1)
#> htmltools 0.3.6 2017-04-28 CRAN (R 3.4.1)
#> knitr 1.19 2018-01-29 CRAN (R 3.4.1)
#> labeling 0.3 2014-08-23 CRAN (R 3.4.0)
#> lazyeval 0.2.1 2017-10-29 CRAN (R 3.4.2)
#> magrittr * 1.5 2014-11-22 CRAN (R 3.4.2)
#> memoise 1.1.0 2017-04-21 CRAN (R 3.4.1)
#> methods * 3.4.1 2017-06-30 local
#> munsell 0.4.3 2016-02-13 CRAN (R 3.4.1)
#> pillar 1.1.0 2018-01-14 CRAN (R 3.4.1)
#> pkgconfig 2.0.1 2017-03-21 CRAN (R 3.4.1)
#> plyr 1.8.4 2016-06-08 CRAN (R 3.4.1)
#> purrr 0.2.4 2017-10-18 CRAN (R 3.4.2)
#> R6 2.2.2 2017-06-17 CRAN (R 3.4.1)
#> Rcpp 0.12.15 2018-01-20 CRAN (R 3.4.1)
#> rlang 0.1.6.9003 2018-02-08 Github (tidyverse/rlang@616fd4d)
#> rmarkdown 1.8 2017-11-17 CRAN (R 3.4.1)
#> rprojroot 1.3-2 2018-01-03 CRAN (R 3.4.1)
#> scales 0.5.0.9000 2018-01-15 Github (hadley/scales@d767915)
#> stats * 3.4.1 2017-06-30 local
#> stringi 1.1.6 2017-11-17 CRAN (R 3.4.2)
#> stringr 1.2.0 2017-02-18 CRAN (R 3.4.1)
#> tibble 1.4.2 2018-01-22 CRAN (R 3.4.1)
#> tools 3.4.1 2017-06-30 local
#> utils * 3.4.1 2017-06-30 local
#> withr 2.1.1.9000 2018-01-15 Github (jimhester/withr@df18523)
#> yaml 2.1.16 2017-12-12 CRAN (R 3.4.3)
由reprex package(v0.1.1.9000)于2018-02-09创建。