Skip to content

Commit

Permalink
grade as factor in ae_plot_grade_max
Browse files Browse the repository at this point in the history
participate in #35
  • Loading branch information
DanChaltiel committed May 16, 2024
1 parent 46773b9 commit 73a0c8f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ importFrom(ggplot2,scale_fill_steps)
importFrom(ggplot2,scale_x_continuous)
importFrom(ggplot2,scale_x_discrete)
importFrom(ggplot2,scale_y_continuous)
importFrom(ggplot2,scale_y_discrete)
importFrom(ggplot2,theme)
importFrom(ggplot2,unit)
importFrom(ggplot2,vars)
Expand Down
24 changes: 11 additions & 13 deletions R/ae_table_grade.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ ae_table_grade_max = function(
#' @inheritParams ae_table_soc
#' @inherit ae_table_soc seealso
#' @param type the plots to be included. One of `c("stack", "dodge", "fill")`.
#' @param drop_levels whether to drop unused grade levels.
#'
#' @return a patchwork of ggplots
#' @importFrom dplyr arrange full_join mutate rename_with select summarise
#' @importFrom ggplot2 aes geom_bar ggplot labs scale_x_continuous theme waiver
#' @importFrom ggplot2 aes geom_bar ggplot labs scale_x_continuous scale_y_discrete theme waiver
#' @importFrom purrr map
#' @importFrom rlang set_names
#' @export
Expand All @@ -88,6 +89,7 @@ ae_table_grade_max = function(
ae_plot_grade_max = function(
df_ae, ..., df_enrol,
type = c("stack", "dodge", "fill"),
drop_levels = FALSE,
arm="ARM", subjid="SUBJID", soc="AESOC", grade="AEGR"
){
check_installed("patchwork", "for `ae_plot_grade_max()` to work")
Expand All @@ -99,25 +101,21 @@ ae_plot_grade_max = function(
select(subjid=any_of2(subjid), arm=any_of2(arm)) %>%
full_join(df_ae, by="subjid") %>%
arrange(subjid) %>%
mutate(grade = ifelse(is.na(soc), 0, fix_grade(grade)) %>% recode("Grade 0"="No AE"))
mutate(grade = ifelse(is.na(soc), 0, fix_grade(grade)))

if(is.null(arm)){
x = a %>%
summarise(grade_max = max_narm(grade), .by=c(subjid)) %>%
mutate(grade_max = ifelse(is.na(grade_max), "NA", paste("Grade", grade_max)))
} else {
x = a %>%
summarise(grade_max = max_narm(grade), .by=c(subjid, arm)) %>%
mutate(grade_max = ifelse(is.na(grade_max), "NA", paste("Grade", grade_max)))
}
by_cols = if(is.null(arm)) "subjid" else c("subjid", "arm")
x = a %>%
summarise(grade_max = max_narm(grade), .by=any_of(by_cols)) %>%
mutate(grade_max = factor(grade_max, levels=0:5, labels=c("No AE", paste("Grade", 1:5))))

if(is.null(arm)) type="stack"
p_list = type %>% set_names() %>%
map(~{
y_lab = if(.x=="fill") "Proportion" else "Count"
p = x %>%
ggplot(aes(y=grade_max, fill=arm, by=factor(grade_max))) +
ggplot(aes(y=grade_max, fill=arm, by=grade_max)) +
geom_bar(position=.x) +
scale_y_discrete(drop=drop_levels) +
scale_x_continuous(labels = if(.x=="fill") scales::percent else waiver()) +
labs(y="Max AE grade experienced", x=y_lab, fill="Treatment")
# StatProp = ggstats:::StatProp
Expand Down Expand Up @@ -202,7 +200,7 @@ ae_table_grade_n = function(
p = crosstable::format_fixed(x/tot, digits, percent=TRUE)
paste0(x, " (", p, ")")
}))
attr(rtn, "by_table")[] = npat[names(npat)!="Total"]
attr(rtn, "by_table")[] = npat[!is.na(names(npat)) & names(npat)!="Total"] #zarb que crosstable oublie les NA dans by_table non?
rtn
}

Expand Down
3 changes: 3 additions & 0 deletions man/ae_plot_grade_max.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 73a0c8f

Please sign in to comment.