Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

289 possibility to pass custom axis label for g_lineplot and g_sphagettiplot #259

Merged
merged 2 commits into from
Oct 16, 2024

Conversation

m7pr
Copy link
Contributor

@m7pr m7pr commented Oct 14, 2024

Alternative to #250 , related to insightsengineering/teal.goshawk#289

Code for g_lineplot
# Example using ADaM structure analysis dataset.

library(stringr)
library(dplyr)
library(nestcolor)

# original ARM value = dose value
arm_mapping <- list(
  "A: Drug X" = "150mg QD", "B: Placebo" = "Placebo", "C: Combination" = "Combination"
)
color_manual <- c("150mg QD" = "thistle", "Placebo" = "orange", "Combination" = "steelblue")
type_manual <- c("150mg QD" = "solid", "Placebo" = "dashed", "Combination" = "dotted")

ADSL <- rADSL %>% filter(!(ARM == "B: Placebo" & AGE < 40))
ADLB <- rADLB
ADLB <- right_join(ADLB, ADSL[, c("STUDYID", "USUBJID")])
var_labels <- lapply(ADLB, function(x) attributes(x)$label)

ADLB <- ADLB %>%
  mutate(AVISITCD = case_when(
    AVISIT == "SCREENING" ~ "SCR",
    AVISIT == "BASELINE" ~ "BL",
    grepl("WEEK", AVISIT) ~
      paste(
        "W",
        trimws(
          substr(
            AVISIT,
            start = 6,
            stop = str_locate(AVISIT, "DAY") - 1
          )
        )
      ),
    TRUE ~ NA_character_
  )) %>%
  mutate(AVISITCDN = case_when(
    AVISITCD == "SCR" ~ -2,
    AVISITCD == "BL" ~ 0,
    grepl("W", AVISITCD) ~ as.numeric(gsub("\\D+", "", AVISITCD)),
    TRUE ~ NA_real_
  )) %>%
  # use ARMCD values to order treatment in visualization legend
  mutate(TRTORD = ifelse(grepl("C", ARMCD), 1,
                         ifelse(grepl("B", ARMCD), 2,
                                ifelse(grepl("A", ARMCD), 3, NA)
                         )
  )) %>%
  mutate(ARM = as.character(arm_mapping[match(ARM, names(arm_mapping))])) %>%
  mutate(ARM = factor(ARM) %>%
           reorder(TRTORD))
attr(ADLB[["ARM"]], "label") <- var_labels[["ARM"]]

attr(ADLB[["AVISITCDN"]], "label") <- "CUSTOM LABEL FOR THE AXIS LABEL"
g_lineplot(
  label = "Line Plot",
  data = ADLB,
  biomarker_var = "PARAMCD",
  biomarker = "CRP",
  value_var = "AVAL",
  trt_group = "ARM",
  shape = NULL,
  time = "AVISITCDN",
  color_manual = color_manual,
  line_type = type_manual,
  median = FALSE,
  hline_arb = c(.9, 1.1, 1.2, 1.5),
  hline_arb_color = c("green", "red", "blue", "pink"),
  hline_arb_label = c("A", "B", "C", "D"),
  xtick = c(0, 1, 5),
  xlabel = c("Baseline", "Week 1", "Week 5"),
  rotate_xlab = FALSE,
  plot_height = 600
)

attr(ADLB[["AVISITCDN"]], "label") <- NULL
g_lineplot(
  label = "Line Plot",
  data = ADLB,
  biomarker_var = "PARAMCD",
  biomarker = "CRP",
  value_var = "AVAL",
  trt_group = "ARM",
  shape = NULL,
  time = "AVISITCDN",
  color_manual = color_manual,
  line_type = type_manual,
  median = FALSE,
  hline_arb = c(.9, 1.1, 1.2, 1.5),
  hline_arb_color = c("green", "red", "blue", "pink"),
  hline_arb_label = c("A", "B", "C", "D"),
  xtick = c(0, 1, 5),
  xlabel = c("Baseline", "Week 1", "Week 5"),
  rotate_xlab = FALSE,
  plot_height = 600
)
Code for `g_sphagettiplot`
# Example using ADaM structure analysis dataset.

library(stringr)

# original ARM value = dose value
arm_mapping <- list(
  "A: Drug X" = "150mg QD", "B: Placebo" = "Placebo", "C: Combination" = "Combination"
)
color_manual <- c("150mg QD" = "#000000", "Placebo" = "#3498DB", "Combination" = "#E74C3C")

ADLB <- rADLB
var_labels <- lapply(ADLB, function(x) attributes(x)$label)
ADLB <- ADLB %>%
  mutate(AVISITCD = case_when(
    AVISIT == "SCREENING" ~ "SCR",
    AVISIT == "BASELINE" ~ "BL",
    grepl("WEEK", AVISIT) ~
      paste(
        "W",
        trimws(
          substr(
            AVISIT,
            start = 6,
            stop = str_locate(AVISIT, "DAY") - 1
          )
        )
      ),
    TRUE ~ NA_character_
  )) %>%
  mutate(AVISITCDN = case_when(
    AVISITCD == "SCR" ~ -2,
    AVISITCD == "BL" ~ 0,
    grepl("W", AVISITCD) ~ as.numeric(gsub("\\D+", "", AVISITCD)),
    TRUE ~ NA_real_
  )) %>%
  # use ARMCD values to order treatment in visualization legend
  mutate(TRTORD = ifelse(grepl("C", ARMCD), 1,
                         ifelse(grepl("B", ARMCD), 2,
                                ifelse(grepl("A", ARMCD), 3, NA)
                         )
  )) %>%
  mutate(ARM = as.character(arm_mapping[match(ARM, names(arm_mapping))])) %>%
  mutate(ARM = factor(ARM) %>%
           reorder(TRTORD)) %>%
  mutate(ANRLO = .5, ANRHI = 1) %>%
  rowwise() %>%
  group_by(PARAMCD) %>%
  mutate(LBSTRESC = ifelse(USUBJID %in% sample(USUBJID, 1, replace = TRUE),
                           paste("<", round(runif(1, min = .5, max = .7))), LBSTRESC
  )) %>%
  mutate(LBSTRESC = ifelse(USUBJID %in% sample(USUBJID, 1, replace = TRUE),
                           paste(">", round(runif(1, min = .9, max = 1.2))), LBSTRESC
  )) %>%
  ungroup()
attr(ADLB[["ARM"]], "label") <- var_labels[["ARM"]]
attr(ADLB[["ANRLO"]], "label") <- "Analysis Normal Range Lower Limit"
attr(ADLB[["ANRHI"]], "label") <- "Analysis Normal Range Upper Limit"

# add LLOQ and ULOQ variables
ADLB_LOQS <- goshawk:::h_identify_loq_values(ADLB, "LOQFL")
ADLB <- left_join(ADLB, ADLB_LOQS, by = "PARAM")
attr(ADLB[["AVISITCD"]], "label") <- "CUSTOM LABEL FOR THE AXIS LABEL"

g_spaghettiplot(
  data = ADLB,
  subj_id = "USUBJID",
  biomarker_var = "PARAMCD",
  biomarker = "CRP",
  value_var = "AVAL",
  trt_group = "ARM",
  time = "AVISITCD",
  color_manual = color_manual,
  color_comb = "#39ff14",
  alpha = .02,
  xtick = c("BL", "W 1", "W 4"),
  xlabel = c("Baseline", "Week 1", "Week 4"),
  rotate_xlab = FALSE,
  group_stats = "median",
  hline_vars = c("ANRHI", "ANRLO"),
  hline_vars_colors = c("pink", "brown")
)

lineplot

Custom label from attribute

image

No attribute, so take column name

image

sphagettiplot

Custom label from attribute

image

No attribute, so take column name

image

Copy link
Contributor

github-actions bot commented Oct 14, 2024

badge

Code Coverage Summary

Filename                           Stmts    Miss  Cover    Missing
-------------------------------  -------  ------  -------  ---------
R/g_boxplot.R                        116     116  0.00%    147-310
R/g_correlationplot.R                137     137  0.00%    251-421
R/g_density_distribution_plot.R       86      86  0.00%    125-236
R/g_lineplot.R                       276     276  0.00%    266-611
R/g_scatterplot.R                    130     130  0.00%    142-310
R/g_spaghettiplot.R                  101     101  0.00%    248-381
R/geom_axes_line.R                   167     167  0.00%    46-358
R/t_summarytable.R                   102     102  0.00%    87-224
R/utils.R                             70      70  0.00%    17-137
TOTAL                               1185    1185  0.00%

Diff against main

Filename          Stmts    Miss  Cover
--------------  -------  ------  --------
R/g_lineplot.R       +1      +1  +100.00%
TOTAL                +1      +1  +100.00%

Results for commit: 2a317a3

Minimum allowed coverage is 80%

♻️ This comment has been updated with latest results

@m7pr m7pr changed the title 289 possibility to pass custom axis label for g_lineplot 289 possibility to pass custom axis label for g_lineplot and g_sphagettiplot Oct 14, 2024
Copy link
Contributor

@donyunardi donyunardi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this alternative @m7pr.

This approach will help us stay consistent with spaghettiplot.

However, I'm not seeing the same result you're getting with spaghettiplot using your example code. For me, it always shows only "AVISITCD."

This seems to happen because this line removes the label attribute:

goshawk/R/g_spaghettiplot.R

Lines 262 to 268 in d8439aa

if (xtype == "discrete") {
data[[time]] <- if (!is.null(time_level)) {
factor(data[[time]], levels = time_level)
} else {
factor(data[[time]])
}
}

Could you please double check?

@m7pr
Copy link
Contributor Author

m7pr commented Oct 15, 2024

Ah sorry @donyunardi - I made a commit locally, but didn't push it.. Pushing it now

@m7pr m7pr requested a review from donyunardi October 15, 2024 07:11
@m7pr m7pr enabled auto-merge (squash) October 16, 2024 07:10
@m7pr m7pr merged commit 19b2c67 into main Oct 16, 2024
25 checks passed
@m7pr m7pr deleted the 289_xlab@main branch October 16, 2024 07:11
@github-actions github-actions bot locked and limited conversation to collaborators Oct 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants