pharmaverse / tidytlg

The goal of tidytlg is to generate tables, listings, and graphs (TLG) using Tidyverse.
https://pharmaverse.github.io/tidytlg/
Other
33 stars 6 forks source link

gentlg.R - Creation of RTF file from multiple plots using ggplot objects #24

Closed vinaypatel-R closed 4 months ago

vinaypatel-R commented 12 months ago

First of all thank you for creating such a great function.

I am trying to use above function to create RTF file for multiple plots.

I have box plot for each subject which I am storing each plot as ggplot object (list), which I combine in the end to create one list containing all individual plot lists.

For example, all_plots = list () all_plots <- list(plotlist1, plotlist2, plotlist3, …)

I am passing this all_plots into huxme parameter in gentlg function but it gives me below error. Looking at the documentation, I can see it is possible to create RTF for multiple plots using PNG files but I am wondering if it is possible with multiple ggplot objects to avoid creating PNG files.

Error: Error in ans[npos] <- rep(no, length.out = len)[npos] : replacement has length zero In addition: Warning message: In rep(no, length.out = len) : 'x' is NULL so the result will be NULL

Function call: gentlg(huxme = all_plots, tlf = "Figure", format = "rtf", orientation = "landscape", file = "reddotplot_tidytlg", title = "Boxplot of individual subjects data", footers = "Produced with tidytlg", watermark = "Confidential")

If I change huxme to “all_plots[[1]]”, it works and creates RTF with one subject plot.

Thank you in advance.

kpagacz commented 4 months ago

Hello there,

so as of this commit: 6ac4f27abb2f99bc4d0658b04152a08005586f15 gentlg supports passing multiple plots to huxme. It's being supported the same way tables and listings are, so each plot lands on its separate page. You can test it out with the below:

library(dplyr)
library(haven)
library(ggplot2)
library(tidytlg)

# read adsl from PhUSE test data factory
testdata <- "[https://github.com/phuse-org/TestDataFactory/raw/main/Updated/TDF_ADaM/"](https://github.com/phuse-org/TestDataFactory/raw/main/Updated/TDF_ADaM/%22)
adsl <- read_xpt(url(paste0(testdata,"adsl.xpt")))

tblid <- "Graph01"

# Process Data --------------------------------------------------------------------------------------------
adsl <- adsl  %>%
  filter(ITTFL == "Y") %>%
  select(USUBJID, ITTFL, TRT01PN, TRT01P, AGE, SEX, HEIGHTBL, WEIGHTBL) %>%
  mutate(SEX = factor(SEX, levels = c("M", "F"), labels = c("Male", "Female")))

# Generate Results ----------------------------------------------------------------------------------------

plot <- ggplot(data = adsl, aes(x = HEIGHTBL, y = WEIGHTBL)) +
  geom_point() +
  labs(x = "Baseline Height (cm)",
       y = "Baseline Weight (kg)") +
  facet_wrap(~SEX, nrow=1)

# create png file
working_dir <- tempdir()
png(file.path(working_dir, paste0(tblid,".png")), width=2800, height=1300, res=300)
plot
dev.off()
#> Warning: Removed 1 row containing missing values or values outside the scale range
#> (`geom_point()`).
gentlg(huxme = list(plot, plot),
tlf = "g",
       plotwidth = 10,
       plotheight = 5,
       orientation = "landscape",
       file = tblid,
       title_file = system.file("extdata/titles.xls", package = "tidytlg"))