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

[Feature Request]: Add facet + cohort legend strata order in mean plot #1212

Closed
3 tasks done
legrasv opened this issue Mar 18, 2024 · 7 comments · Fixed by #1226
Closed
3 tasks done

[Feature Request]: Add facet + cohort legend strata order in mean plot #1212

legrasv opened this issue Mar 18, 2024 · 7 comments · Fixed by #1226
Assignees

Comments

@legrasv
Copy link
Contributor

legrasv commented Mar 18, 2024

Feature description

Dear NEST,

For a project, we needed to generate a mean plot by Nominal dose (group), but facet by COUNTRY. There was no way of adding a facet to the plot aftewards as the data in the plot is the summarized data with the mean and sd, and the column COUNTRY is lost. We had to manually modify the function so that a second grouping column is kept:

image

These are the code we added to g_lineplot:

`#'   * `facetvar` (`character`)\cr name of grouping variable, i.e. treatment arm. Can be `NA` to indicate lack of groups.



  if (is.na(variables["facetvar"])) {
    facetvar <- NULL # NULL if facetvar == NA or it is not in variables
  } else {
    facetvar <- variables[["facetvar"]]
  }




  ####################################### |
  # ---- Compute required statistics ----
  ####################################### |
  if (!is.null(facetvar) & !is.null(strata)) {
    df_grp <- tidyr::expand(df, .data[[facetvar]], .data[[strata]], .data[[x]]) # expand based on levels of factors
  } else  if (!is.null(strata)) {
    df_grp <- tidyr::expand(df, NULL, .data[[strata]], .data[[x]]) # expand based on levels of factors
  } else {
    df_grp <- tidyr::expand(df, NULL, NULL, .data[[x]])
  }
  df_grp <- df_grp %>%
    dplyr::full_join(y = df[, c(facetvar, strata, x, y)], by = c(facetvar, strata, x), multiple = "all") %>%
    dplyr::group_by_at(c(facetvar, strata, x))



#' @export
control_lineplot_vars <- function(x = "AVISIT", y = "AVAL", strata = "ARM", paramcd = "PARAMCD", y_unit = "AVALU",
                                  cohort_id = "USUBJID", facetvar=NA) {
  checkmate::assert_string(x)
  checkmate::assert_string(y)
  checkmate::assert_string(strata, na.ok = TRUE)
  checkmate::assert_string(cohort_id, na.ok = TRUE)
  checkmate::assert_string(paramcd, na.ok = TRUE)
  checkmate::assert_string(y_unit, na.ok = TRUE)
  checkmate::assert_string(facetvar, na.ok = TRUE)
  
  variables <- c(x = x, y = y, strata = strata, paramcd = paramcd, y_unit = y_unit, cohort_id = cohort_id, facetvar=facetvar)
  return(variables)
}

And then with the parameter facetvar in the plot, we could produce our plot.

Would it be possible to code something similar to allow us to produce facets on this plot? please

also the legend was incorrect when using strata:
image
Could you check this?
issue raised a month ago:
https://insightsengineering.github.io/tlg-catalog/stable/graphs/pharmacokinetic/pkcg03.html

Best regards
Valentin

Code of Conduct

  • I agree to follow this project's Code of Conduct.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines.

Security Policy

  • I agree to follow this project's Security Policy.
@legrasv legrasv changed the title [Feature Request]: Add facet in mean plot [Feature Request]: Add facet + cohort legend strata order in mean plot Mar 18, 2024
@Melkiades Melkiades added the sme label Mar 18, 2024
@ayogasekaram ayogasekaram self-assigned this Apr 10, 2024
@ayogasekaram
Copy link
Contributor

Hi @legrasv, I have a PR going up for this soon so I can tag you for review. I just wanted to confirm if you have any warning message in mind in the case of the alt_counts_df? for example, if the levels for the facet_var are set in adpc for example, but the user also supplied an alt_counts_df, the same levels need to be set there for the plot legend to reflect it.

@legrasv
Copy link
Contributor Author

legrasv commented Apr 15, 2024

@ayogasekaram Thank you for working on that, a colleague will test the new function shortly.
For your question, I believe that alt_counts_df is always equal to df (in our case), I don't see any case when you would like to put two different datasets in the two arguments. So I think we are fine from this side, I don't think we need a warning message.

@legrasv
Copy link
Contributor Author

legrasv commented Apr 15, 2024

@ayogasekaram I got this error message which I can't debug, it looks like I don't have the function sfun loaded (when I am using browser()), what is this function? how can I get it?
image

@ayogasekaram
Copy link
Contributor

@legrasv the sfun is s_summary but that default should already be set in the g_lineplot parameters. Let me take a look on my end as well!

ayogasekaram added a commit that referenced this issue Apr 16, 2024
…ls. (#1226)

closes #1212

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Emily de la Rua <emily.de_la_rua@contractors.roche.com>
@legrasv
Copy link
Contributor Author

legrasv commented Apr 17, 2024

Dear NEST,

I found a bug in the code of g_lineplot for the strata:

matches <- sapply(unique(df_N[[group_var]]), function(x) unique(df_N[[paste0(group_var, "_N")]])[grepl(paste0("^", x), unique(df_N[[paste0(group_var, "_N")]]))])

in this piece of code, it happens that group_var contains specific regex characters and therefore the grepl will not work, example:

Browse[2]> grepl("^PART 1 \\(Period Effect\\)", unique(df_N[[paste0(group_var, "_N")]]))
[1]  TRUE  TRUE FALSE FALSE
Browse[2]> grepl("^PART 1 (Period Effect)", unique(df_N[[paste0(group_var, "_N")]]))
[1] FALSE FALSE FALSE FALSE

-> I need to tell that "()" are not part of the regex expression. I don't know how we could handle that in general. Here I have the bug with the "(", but it could happen with regex characters such as "+", ".", "[", etc.

@legrasv legrasv reopened this Apr 17, 2024
ayogasekaram added a commit that referenced this issue Apr 26, 2024
update g_lineplot code to handle special characters in group_var. add
test for maintaining factor levels.

related to #1212

---------

Signed-off-by: Abinaya Yogasekaram <73252787+ayogasekaram@users.noreply.github.com>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
@edelarua
Copy link
Contributor

@ayogasekaram can this issue be closed now?

@ayogasekaram
Copy link
Contributor

yes it's been addressed in this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants