kassambara / ggpubr

'ggplot2' Based Publication Ready Plots

Home Page:https://rpkgs.datanovia.com/ggpubr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ggsave() does not include the annotated texts

tdlan80 opened this issue · comments

I have produced several plots using ggpubr and then used ggarrange to make a grid of each subplot. Then, I added additional texts to the gird with annotate_figure. All these steps worked with no issue

plot_allClassDEP <- plot_allClassDEP %>% annotate_figure(top = text_grob(label = "NDVI-based index, DEP classes, smooth5", color = "black", face = "bold", size = 10, just ="center" ), bottom = text_grob(label = "pairwise comparisons: Wilcoxon test, p.adjust = Bonferroni", color = "black", face = "plain", size = 10, just = "right" ), left = NULL, right = NULL, fig.lab = "Fig #", fig.lab.pos = c("top.left"), fig.lab.size = 10, fig.lab.face = "plain")

Finally, I saved this output with ggsave.

ggsave2(plot_allClassDEP, filename = "figsTabs/plot_allClassDEP.png", device = "png", dpi = 300, limitsize = F, 
        width = 14,  height = 11)

The figure saved does not include the texts I annotated.

I also tried ggsave2 and save_plot.

save_plot(filename = "figsTabs/plot_allClassDEP.png", plot = plot_allClassDEP, ncol = 4, nrow = 2,
  base_height = 3.71, base_asp = 1.618, base_width = NULL, 
  device = "png", dpi = 300, limitsize = F)

Still, my saved image of the grid does not include the annotations. When I look at the figure through the plots window of Rstudio, I can see the annotations. But, this is dropped when I save it.

However, if i change the device arg in ggsave to pdf, I get to save the annotation as well. PNG for instance, does not work as it omits the annotations

session_info()

R version 4.2.3 (2023-03-15 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C LC_TIME=English_United States.utf8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] cowplot_1.1.1 ggtext_0.1.2 stringi_1.7.12 glue_1.6.2 ggpubr_0.6.0 rstatix_0.7.2
[7] gridExtra_2.3 ggrepel_0.9.3 MASS_7.3-58.3 writexl_1.4.2 fs_1.6.1 statsExpressions_1.5.0
[13] ggstatsplot_0.11.1 jmuOutlier_2.2 emmeans_1.8.5 rcompanion_2.4.26 XNomial_1.0.4 coin_1.4-2
[19] survival_3.5-5 hablar_0.3.2 magrittr_2.0.3 janitor_2.2.0 lubridate_1.9.2 forcats_1.0.0
[25] stringr_1.5.0 dplyr_1.1.1 purrr_1.0.1 tidyr_1.3.0 tibble_3.2.1 ggplot2_3.4.2
[31] tidyverse_2.0.0 readr_2.1.4 readxl_1.4.2

# 
devtools::session_info()

setting value
version R version 4.2.3 (2023-03-15 ucrt)
os Windows 10 x64 (build 19044)
system x86_64, mingw32
ui RStudio
language (EN)
collate English_United States.utf8
ctype English_United States.utf8
tz America/New_York
date 2023-04-16
rstudio 2023.03.0+386 Cherry Blossom (desktop)
pandoc NA

─ Packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
package * version date (UTC) lib source
abind 1.4-5 2016-07-21 [1] CRAN (R 4.2.0)
backports 1.4.1 2021-12-13 [1] CRAN (R 4.2.0)
bayestestR 0.13.1 2023-04-07 [1] CRAN (R 4.2.3)
boot 1.3-28.1 2022-11-22 [1] CRAN (R 4.2.3)
broom 1.0.4 2023-03-11 [1] CRAN (R 4.2.2)
broom.mixed 0.2.9.4 2022-04-17 [1] CRAN (R 4.2.0)
cachem 1.0.7 2023-02-24 [1] CRAN (R 4.2.2)
callr 3.7.3 2022-11-02 [1] CRAN (R 4.2.2)
car 3.1-2 2023-03-30 [1] CRAN (R 4.2.3)
carData 3.0-5 2022-01-06 [1] CRAN (R 4.2.0)
cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.2.0)
checkmate 2.1.0 2022-04-21 [1] CRAN (R 4.2.0)
class 7.3-21 2023-01-23 [1] CRAN (R 4.2.3)
cli 3.6.1 2023-03-23 [1] CRAN (R 4.2.3)
coda 0.19-4 2020-09-30 [1] CRAN (R 4.2.0)
codetools 0.2-19 2023-02-01 [1] CRAN (R 4.2.3)
coin * 1.4-2 2021-10-08 [1] CRAN (R 4.2.0)
colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.2.2)
correlation 0.8.4 2023-04-06 [1] CRAN (R 4.2.3)
cowplot * 1.1.1 2020-12-30 [1] CRAN (R 4.2.0)
crayon 1.5.2 2022-09-29 [1] CRAN (R 4.2.1)
data.table 1.14.8 2023-02-17 [1] CRAN (R 4.2.2)
datawizard 0.7.1 2023-04-03 [1] CRAN (R 4.2.3)
DescTools 0.99.48 2023-02-19 [1] CRAN (R 4.2.2)
devtools 2.4.5 2022-10-11 [1] CRAN (R 4.2.1)
digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.2)
dplyr * 1.1.1 2023-03-22 [1] CRAN (R 4.2.3)
e1071 1.7-13 2023-02-01 [1] CRAN (R 4.2.2)
ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.0)
emmeans * 1.8.5 2023-03-08 [1] CRAN (R 4.2.2)
estimability 1.4.1 2022-08-05 [1] CRAN (R 4.2.1)
Exact 3.2 2022-09-25 [1] CRAN (R 4.2.1)
expm 0.999-7 2023-01-09 [1] CRAN (R 4.2.2)
fansi 1.0.4 2023-01-22 [1] CRAN (R 4.2.2)
farver 2.1.1 2022-07-06 [1] CRAN (R 4.2.1)
fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.2.2)
forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.2.2)
fs * 1.6.1 2023-02-06 [1] CRAN (R 4.2.2)
furrr 0.3.1 2022-08-15 [1] CRAN (R 4.2.1)
future 1.32.0 2023-03-07 [1] CRAN (R 4.2.2)
generics 0.1.3 2022-07-05 [1] CRAN (R 4.2.1)
ggcorrplot 0.1.4 2022-09-27 [1] CRAN (R 4.2.1)
ggplot2 * 3.4.2 2023-04-03 [1] CRAN (R 4.2.3)
ggpubr * 0.6.0 2023-02-10 [1] CRAN (R 4.2.2)
ggrepel * 0.9.3 2023-02-03 [1] CRAN (R 4.2.2)
ggsignif 0.6.4 2022-10-13 [1] CRAN (R 4.2.1)
ggstatsplot * 0.11.1 2023-04-14 [1] CRAN (R 4.2.3)
ggtext * 0.1.2 2022-09-16 [1] CRAN (R 4.2.3)
gld 2.6.6 2022-10-23 [1] CRAN (R 4.2.2)
globals 0.16.2 2022-11-21 [1] CRAN (R 4.2.2)
glue * 1.6.2 2022-02-24 [1] CRAN (R 4.2.2)
gridExtra * 2.3 2017-09-09 [1] CRAN (R 4.2.0)
gridtext 0.1.5 2022-09-16 [1] CRAN (R 4.2.3)
gtable 0.3.3 2023-03-21 [1] CRAN (R 4.2.3)
gtools 3.9.4 2022-11-27 [1] CRAN (R 4.2.2)
hablar * 0.3.2 2023-03-12 [1] CRAN (R 4.2.2)
hms 1.1.3 2023-03-21 [1] CRAN (R 4.2.3)
htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.2.3)
htmlwidgets 1.6.2 2023-03-17 [1] CRAN (R 4.2.3)
httpuv 1.6.9 2023-02-14 [1] CRAN (R 4.2.2)
httr 1.4.5 2023-02-24 [1] CRAN (R 4.2.2)
insight 0.19.1 2023-03-18 [1] CRAN (R 4.2.3)
janitor * 2.2.0 2023-02-02 [1] CRAN (R 4.2.2)
jmuOutlier * 2.2 2019-08-05 [1] CRAN (R 4.2.0)
jsonlite 1.8.4 2022-12-06 [1] CRAN (R 4.2.2)
labeling 0.4.2 2020-10-20 [1] CRAN (R 4.2.0)
later 1.3.0 2021-08-18 [1] CRAN (R 4.2.0)
lattice 0.21-8 2023-04-05 [1] CRAN (R 4.2.3)
libcoin 1.0-9 2021-09-27 [1] CRAN (R 4.2.0)
lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.1)
listenv 0.9.0 2022-12-16 [1] CRAN (R 4.2.2)
lmom 2.9 2022-05-29 [1] CRAN (R 4.2.0)
lmtest 0.9-40 2022-03-21 [1] CRAN (R 4.2.0)
lubridate * 1.9.2 2023-02-10 [1] CRAN (R 4.2.2)
magrittr * 2.0.3 2022-03-30 [1] CRAN (R 4.2.2)
MASS * 7.3-58.3 2023-03-07 [1] CRAN (R 4.2.3)
Matrix 1.5-4 2023-04-04 [1] CRAN (R 4.2.3)
matrixStats 0.63.0 2022-11-18 [1] CRAN (R 4.2.2)
mctest 1.3.1 2020-06-29 [1] CRAN (R 4.2.0)
memoise 2.0.1 2021-11-26 [1] CRAN (R 4.2.0)
mime 0.12 2021-09-28 [1] CRAN (R 4.2.0)
miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 4.2.0)
modeltools 0.2-23 2020-03-05 [1] CRAN (R 4.2.0)
multcomp 1.4-23 2023-03-09 [1] CRAN (R 4.2.2)
multcompView 0.1-9 2023-04-09 [1] CRAN (R 4.2.3)
munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.0)
mvtnorm 1.1-3 2021-10-08 [1] CRAN (R 4.2.0)
neonUtilities 2.2.1 2023-03-10 [1] CRAN (R 4.2.2)
nlme 3.1-162 2023-01-31 [1] CRAN (R 4.2.3)
nortest 1.0-4 2015-07-30 [1] CRAN (R 4.2.0)
paletteer 1.5.0 2022-10-19 [1] CRAN (R 4.2.1)
parallelly 1.35.0 2023-03-23 [1] CRAN (R 4.2.3)
parameters 0.20.3 2023-04-05 [1] CRAN (R 4.2.3)
patchwork 1.1.2 2022-08-19 [1] CRAN (R 4.2.1)
pillar 1.9.0 2023-03-22 [1] CRAN (R 4.2.3)
pkgbuild 1.4.0 2022-11-27 [1] CRAN (R 4.2.2)
pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0)
pkgload 1.3.2 2022-11-16 [1] CRAN (R 4.2.2)
plyr 1.8.8 2022-11-11 [1] CRAN (R 4.2.2)
ppcor 1.1 2015-12-03 [1] CRAN (R 4.2.0)
prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.2.0)
processx 3.8.0 2022-10-26 [1] CRAN (R 4.2.2)
profvis 0.3.7 2020-11-02 [1] CRAN (R 4.2.1)
promises 1.2.0.1 2021-02-11 [1] CRAN (R 4.2.0)
proxy 0.4-27 2022-06-09 [1] CRAN (R 4.2.1)
ps 1.7.4 2023-04-02 [1] CRAN (R 4.2.3)
purrr * 1.0.1 2023-01-10 [1] CRAN (R 4.2.2)
R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0)
ragg 1.2.5 2023-01-12 [1] CRAN (R 4.2.2)
rcompanion * 2.4.26 2023-04-03 [1] CRAN (R 4.2.3)
Rcpp 1.0.10 2023-01-22 [1] CRAN (R 4.2.2)
readr * 2.1.4 2023-02-10 [1] CRAN (R 4.2.2)
readxl * 1.4.2 2023-02-09 [1] CRAN (R 4.2.2)
rematch2 2.1.2 2020-05-01 [1] CRAN (R 4.2.0)
remotes 2.4.2 2021-11-30 [1] CRAN (R 4.2.0)
rlang 1.1.0 2023-03-14 [1] CRAN (R 4.2.3)
rootSolve 1.8.2.3 2021-09-29 [1] CRAN (R 4.2.0)
rstatix * 0.7.2 2023-02-01 [1] CRAN (R 4.2.2)
rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.1)
sandwich 3.0-2 2022-06-15 [1] CRAN (R 4.2.1)
scales 1.2.1 2022-08-20 [1] CRAN (R 4.2.1)
sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0)
shiny 1.7.4 2022-12-15 [1] CRAN (R 4.2.2)
snakecase 0.11.0 2019-05-25 [1] CRAN (R 4.2.0)
statsExpressions * 1.5.0 2023-02-19 [1] CRAN (R 4.2.2)
stringi * 1.7.12 2023-01-11 [1] CRAN (R 4.2.2)
stringr * 1.5.0 2022-12-02 [1] CRAN (R 4.2.2)
survival * 3.5-5 2023-03-12 [1] CRAN (R 4.2.3)
systemfonts 1.0.4 2022-02-11 [1] CRAN (R 4.2.0)
tagger 0.0.0.9000 2023-04-06 [1] Github (eliocamp/tagger@4ba9573)
textshaping 0.3.6 2021-10-13 [1] CRAN (R 4.2.1)
TH.data 1.1-1 2022-04-26 [1] CRAN (R 4.2.0)
tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.2.3)
tidyr * 1.3.0 2023-01-24 [1] CRAN (R 4.2.2)
tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.2.1)
tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.2.2)
timechange 0.2.0 2023-01-11 [1] CRAN (R 4.2.2)
tzdb 0.3.0 2022-03-28 [1] CRAN (R 4.2.0)
urlchecker 1.0.1 2021-11-30 [1] CRAN (R 4.2.1)
usethis 2.1.6 2022-05-25 [1] CRAN (R 4.2.0)
utf8 1.2.3 2023-01-31 [1] CRAN (R 4.2.2)
vctrs 0.6.1 2023-03-22 [1] CRAN (R 4.2.3)
withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0)
writexl * 1.4.2 2023-01-06 [1] CRAN (R 4.2.2)
xml2 1.3.3 2021-11-30 [1] CRAN (R 4.2.0)
XNomial * 1.0.4 2015-12-24 [1] CRAN (R 4.2.0)
xtable 1.8-4 2019-04-21 [1] CRAN (R 4.2.0)
zeallot 0.1.0 2018-01-28 [1] CRAN (R 4.2.0)
zoo 1.8-11 2022-09-17 [1] CRAN (R 4.2.1)

I was having what seemed like the same issue. I was trying to save a tiff but even a pdf didn't include the annotations. I'm not sure why, but it seems like ggsave is pulling the ggplot object and annotate_figure only modified the figure display, not the ggplot object itself. I found a workaround by running annotate_figure in a pipe in the "plot" argument of ggsave. So:

ggsave("yourPlot.png", plot = yourPlot %>% annotate_figure(yourPlot, arguments you want), limitsize = F,
width = 14, height = 11)