Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Saving Figures as PDF #587

Closed
mhulke opened this issue Apr 5, 2023 · 2 comments
Closed

Saving Figures as PDF #587

mhulke opened this issue Apr 5, 2023 · 2 comments

Comments

@mhulke
Copy link

mhulke commented Apr 5, 2023

Hello sqjin,

I was hoping you could offer some advice for saving plots from the main analysis. I have read through the many posts regarding how to save a figure, but none of the solutions seem to work. I noticed that most functions output an image as recordedplot, and I haven't been able to find anything online about saving a recordedplot as a pdf. I attempted to convert the recordedplot to a ggplot class, but the function as.grob() gives as error that 'as.grob' doesn't work on the class recordedplot. Similarly, I was not able to save any plots using ggsave (as is done in the tutorial) since the images aren't ggplot class. When I try using pdf() devoff() or svglite() dev.off() using the code:

pdf('test_plot.pdf')
netVisual_circle(cellchat@net$count, vertex.weight = groupSize, weight.scale = T, label.edge= F, title.name = "Number of Interactions")
dev.off()

I get the following error:
ERROR while rich displaying an object: Error in file(con, "rb"): cannot open the connection

Traceback:

  1. tryCatch(withCallingHandlers({
    . if (!mime %in% names(repr::mime2repr))
    . stop("No repr_* for mimetype ", mime, " in repr::mime2repr")
    . rpr <- repr::mime2repr[mime]
    . if (is.null(rpr))
    . return(NULL)
    . prepare_content(is.raw(rpr), rpr)
    . }, error = error_handler), error = outer_handler)
  2. tryCatchList(expr, classes, parentenv, handlers)
  3. tryCatchOne(expr, names, parentenv, handlers[[1L]])
  4. doTryCatch(return(expr), name, parentenv, handler)
  5. withCallingHandlers({
    . if (!mime %in% names(repr::mime2repr))
    . stop("No repr_* for mimetype ", mime, " in repr::mime2repr")
    . rpr <- repr::mime2repr[mime]
    . if (is.null(rpr))
    . return(NULL)
    . prepare_content(is.raw(rpr), rpr)
    . }, error = error_handler)
  6. repr::mime2repr[mime]
  7. repr_png.recordedplot(obj)
  8. repr_recordedplot_generic(obj, ".png", TRUE, dev.cb)
  9. readBin(tf, raw(), file.info(tf)$size)
  10. file(con, "rb")
    ERROR while rich displaying an object: Error in file(con, "rb"): cannot open the connection

Traceback:

  1. tryCatch(withCallingHandlers({
    . if (!mime %in% names(repr::mime2repr))
    . stop("No repr_* for mimetype ", mime, " in repr::mime2repr")
    . rpr <- repr::mime2repr[mime]
    . if (is.null(rpr))
    . return(NULL)
    . prepare_content(is.raw(rpr), rpr)
    . }, error = error_handler), error = outer_handler)
  2. tryCatchList(expr, classes, parentenv, handlers)
  3. tryCatchOne(expr, names, parentenv, handlers[[1L]])
  4. doTryCatch(return(expr), name, parentenv, handler)
  5. withCallingHandlers({
    . if (!mime %in% names(repr::mime2repr))
    . stop("No repr_* for mimetype ", mime, " in repr::mime2repr")
    . rpr <- repr::mime2repr[mime]
    . if (is.null(rpr))
    . return(NULL)
    . prepare_content(is.raw(rpr), rpr)
    . }, error = error_handler)
  6. repr::mime2repr[mime]
  7. repr_jpg.recordedplot(obj)
  8. repr_recordedplot_generic(obj, ".jpg", TRUE, dev.cb)
  9. readBin(tf, raw(), file.info(tf)$size)
  10. file(con, "rb")

I am working in an R notebook in app.terra. My session information:
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
[1] forcats_1.0.0 stringr_1.5.0 purrr_1.0.1
[4] readr_2.1.3 tidyr_1.3.0 tibble_3.2.1
[7] tidyverse_1.3.2 ggraph_2.1.0 ggplotify_0.1.0
[10] svglite_2.1.1 reticulate_1.28 circlize_0.4.16
[13] patchwork_1.1.2 CellChat_1.6.1 Biobase_2.58.0
[16] BiocGenerics_0.44.0 ggplot2_3.4.2 igraph_1.4.1
[19] dplyr_1.1.1

loaded via a namespace (and not attached):
[1] readxl_1.4.1 uuid_1.1-0 backports_1.4.1
[4] systemfonts_1.0.4 NMF_0.26 plyr_1.8.8
[7] repr_1.1.4 BiocParallel_1.32.4 listenv_0.9.0
[10] ggnetwork_0.5.12 gridBase_0.4-7 digest_0.6.31
[13] foreach_1.5.2 yulab.utils_0.0.6 htmltools_0.5.4
[16] viridis_0.6.2 ggalluvial_0.12.5 fansi_1.0.4
[19] magrittr_2.0.3 googlesheets4_1.0.1 cluster_2.1.4
[22] doParallel_1.0.17 tzdb_0.3.0 sna_2.7-1
[25] ComplexHeatmap_2.15.3 globals_0.16.2 graphlayouts_0.8.4
[28] modelr_0.1.10 matrixStats_0.63.0 timechange_0.1.1
[31] colorspace_2.1-0 rvest_1.0.3 rappdirs_0.3.3
[34] ggrepel_0.9.3 haven_2.5.1 textshaping_0.3.6
[37] crayon_1.5.2 jsonlite_1.8.4 iterators_1.0.14
[40] glue_1.6.2 polyclip_1.10-4 registry_0.5-1
[43] gtable_0.3.3 gargle_1.2.1 GetoptLong_1.0.5
[46] car_3.1-2 future.apply_1.10.0 shape_1.4.6
[49] abind_1.4-5 scales_1.2.1 DBI_1.1.3
[52] rngtools_1.5.2 rstatix_0.7.2 Rcpp_1.0.10
[55] viridisLite_0.4.1 clue_0.3-64 gridGraphics_0.5-1
[58] stats4_4.2.2 httr_1.4.4 FNN_1.1.3.2
[61] RColorBrewer_1.1-3 ellipsis_0.3.2 pkgconfig_2.0.3
[64] farver_2.1.1 dbplyr_2.2.1 utf8_1.2.3
[67] here_1.0.1 tidyselect_1.2.0 labeling_0.4.2
[70] rlang_1.1.0 reshape2_1.4.4 cellranger_1.1.0
[73] munsell_0.5.0 tools_4.2.2 cli_3.6.1
[76] generics_0.1.3 statnet.common_4.8.0 broom_1.0.4
[79] evaluate_0.20 fastmap_1.1.0 ragg_1.2.4
[82] fs_1.6.1 tidygraph_1.2.3 pbapply_1.7-0
[85] future_1.32.0 xml2_1.3.3 compiler_4.2.2
[88] png_0.1-8 ggsignif_0.6.4 reprex_2.0.2
[91] tweenr_2.0.2 stringi_1.7.12 RSpectra_0.16-1
[94] lattice_0.20-45 IRdisplay_1.1 Matrix_1.5-3
[97] vctrs_0.6.1 pillar_1.9.0 lifecycle_1.0.3
[100] BiocManager_1.30.20 GlobalOptions_0.1.2 BiocNeighbors_1.16.0
[103] cowplot_1.1.1 irlba_2.3.5.1 R6_2.5.1
[106] network_1.18.1 gridExtra_2.3 IRanges_2.32.0
[109] parallelly_1.35.0 codetools_0.2-18 MASS_7.3-58.1
[112] assertthat_0.2.1 rprojroot_2.0.3 rjson_0.2.21
[115] withr_2.5.0 S4Vectors_0.36.2 hms_1.1.2
[118] parallel_4.2.2 IRkernel_1.3.1 coda_0.19-4
[121] carData_3.0-5 googledrive_2.0.0 ggpubr_0.6.0
[124] pbdZMQ_0.3-8 ggforce_0.4.1 lubridate_1.9.0
[127] base64enc_0.1-3

Thank you for any help you can offer.

@pwmellors
Copy link

Try this - with this approach you need to specify a filepath for the pdf (I made one up), make a plot object from the netVisual function and then repeat the plot object name prior to dev.off()

pdf('/users/myname/myfiles/test_plot.pdf')
testplot <- netVisual_circle(cellchat@net$count, vertex.weight = groupSize, weight.scale = T, label.edge= F, title.name = "Number of Interactions")
testplot
dev.off()

@mhulke
Copy link
Author

mhulke commented Apr 7, 2023

This worked! Thank you for the help!

@mhulke mhulke closed this as completed Apr 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants