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

addresses #578 - use dplyr::select insstead of select_ #579

Merged
merged 11 commits into from
Oct 29, 2024
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ License: GPL-2
LazyData: TRUE
Encoding: UTF-8
Depends:
ggplot2,
ggplot2(>= 3.4.0),
ggpubr(>= 0.1.6)
Imports:
grid,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ importFrom(grDevices,axisTicks)
importFrom(magrittr,"%>%")
importFrom(methods,is)
importFrom(purrr,map)
importFrom(rlang,"!!")
importFrom(rlang,sym)
importFrom(rlang,syms)
importFrom(stats,anova)
importFrom(stats,approx)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## Minor changes

- R-ADDICT website is no longer live, so updating links in README (#622)
- ggplot2 minimum version is now 3.4.0

## Bug fixes

Expand Down
11 changes: 6 additions & 5 deletions R/ggcoxdiagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,20 @@ ggcoxdiagnostics <- function (fit,
else col_names <- names(stats::coef(fit))
colnames(res) <- col_names
res$xval <- xval
data2plot <- tidyr::gather_(res,
key_col = "covariate", value_col = "res",
gather_col = col_names)
data2plot <- tidyr::pivot_longer(
data = res, cols = dplyr::all_of(col_names),
names_to = "covariate", values_to = "res"
)

gplot <- ggplot(aes(xval, res), data = data2plot) +
geom_point(col = point.col, shape = point.shape,
size = point.size, alpha = point.alpha)

if (hline) gplot <- gplot + geom_hline(yintercept=hline.yintercept, col = hline.col,
size = hline.size, lty = hline.lty, alpha = hline.alpha)
linewidth = hline.size, lty = hline.lty, alpha = hline.alpha)

if (sline) gplot <- gplot + geom_smooth(col = sline.col, se = sline.se, method = "loess",
size = sline.size, lty = sline.lty, alpha = sline.alpha)
linewidth = sline.size, lty = sline.lty, alpha = sline.alpha)

gplot <- gplot + labs(x = xlabel, y = ylabel, title = title, subtitle = subtitle, caption = caption) + ggtheme
# customization
Expand Down
12 changes: 6 additions & 6 deletions R/ggsurvplot_combine.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ ggsurvplot_combine <- function(fit, data,
all.levels <- c(all.levels, .levels(ss$strata))
# convert strata into character before binding
# avoid this warning: Unequal factor levels: coercing to character
grouped.d$survsummary <- map(grouped.d$survsummary,
grouped.d$survsummary <- purrr::map(grouped.d$survsummary,
function(x){
x$strata <- as.character(x$strata)
x
Expand Down Expand Up @@ -161,9 +161,9 @@ ggsurvplot_combine <- function(fit, data,
survtable$strata <- paste(fitname, "::", survtable$strata, sep = "") %>%
factor(levels = strata.levels)
survtable %>%
dplyr::select_( .dots = c("strata", "time", "n.risk", "pct.risk",
"n.event", "cum.n.event", "n.censor",
"cum.n.censor", "strata_size"))
dplyr::select( dplyr::all_of(c("strata", "time", "n.risk", "pct.risk",
"n.event", "cum.n.event", "n.censor",
"cum.n.censor", "strata_size")))
}

grouped.d <- grouped.d %>%
Expand Down Expand Up @@ -199,8 +199,8 @@ ggsurvplot_combine <- function(fit, data,
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

if(keep.data){
res$data.survplot <- tibble::as.tibble(all.survsummary)
res$data.survtable <- tibble::as.tibble(all.survtable)
res$data.survplot <- tibble::as_tibble(all.survsummary)
res$data.survtable <- tibble::as_tibble(all.survtable)
}


Expand Down
2 changes: 1 addition & 1 deletion R/ggsurvplot_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ ggsurvplot_df <- function(fit, fun = NULL,
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
df[, .strata.var] <- factor( df[, .strata.var], levels = .levels(.strata), labels = legend.labs)

p <- ggplot2::ggplot(df, ggplot2::aes_string("time", "surv")) +
p <- ggplot2::ggplot(df, ggplot2::aes(x = !!sym("time"), y = !!sym("surv"))) +
ggpubr::geom_exec(surv.geom, data = df, size = size, color = color, linetype = linetype, ...) +
ggplot2::scale_y_continuous(breaks = y.breaks, labels = scale_labels, limits = ylim, expand = .expand) +
ggplot2::coord_cartesian(xlim = xlim)+
Expand Down
4 changes: 2 additions & 2 deletions R/ggsurvplot_facet.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ ggsurvplot_facet <- function(fit, data, facet.by,
pvalue <- surv_pvalue(grouped.d$fit, grouped.d$data, pval.coord = pval.coord,
pval.method.coord = pval.method.coord,...) %>%
dplyr::bind_rows() %>%
tibble::as.tibble()
tibble::as_tibble()
# Select the grouping variable columns and cbind the corresponding pvalue
pvals.df <- grouped.d %>%
dplyr::select_( .dots = facet.by) %>%
dplyr::select(!!!syms(facet.by)) %>%
dplyr::bind_cols(pvalue)
pval.x <- pval.y <- pval.txt <- method.x <- method.y <- method <- NULL
p <- p +
Expand Down
2 changes: 1 addition & 1 deletion R/surv_median.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ surv_median <- function(fit, combine = FALSE){
.table$strata <- rownames(.table)

.table <- .table %>%
dplyr::select_(.dots = c("strata", "median", "`0.95LCL`", "`0.95UCL`"))
dplyr::select(dplyr::all_of(c("strata", "median", "0.95LCL", "0.95UCL")))
colnames(.table) <- c("strata", "median", "lower", "upper")
rownames(.table) <- NULL
.table
Expand Down
1 change: 1 addition & 0 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' @importFrom stats pchisq
#' @importFrom survMisc ten comp
#' @importFrom utils capture.output
#' @importFrom rlang !! sym


# Count the number of ggplots in a list
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/setup-load_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
library(survival)
data('cancer')
16 changes: 16 additions & 0 deletions tests/testthat/test-ggcoxdiagnostics.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test_that('ggcoxdiagnostics creates plot with all the observations', {
cph <- coxph(Surv(futime, fustat) ~ rx + age, data=ovarian)
p <- ggcoxdiagnostics(cph, type="deviance")
.build <- ggplot_build(p)
expect_equal(nrow(.build$data[[1]]), nrow(ovarian))
})

test_that('ggcoxdiagnostics with second type two rows for each observed event*term', {
cph <- coxph(Surv(futime, fustat) ~ rx + age, data=ovarian)
qty_terms <- length(attr(terms(cph$formula), "term.labels"))
qty_events <- sum(ovarian$fustat==1)
p <- ggcoxdiagnostics(cph, type="schoenfeld")
.build <- ggplot_build(p)
expect_equal(nrow(.build$data[[1]]), qty_terms*qty_events)
})

21 changes: 21 additions & 0 deletions tests/testthat/test-ggsurvplot_combine.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
start_time <- 250
fit1 <- survfit(Surv(time, status) ~ sex, data = lung)
fit2 <- survfit(Surv(time, status) ~ sex, data = lung, start.time = start_time)

test_that("survplot_combine plots successfully into 4 lines; second 2 fits have only (0,1) before start_time", {
p <- ggsurvplot_combine(list(
original=fit1, conditional=fit2
), data = lung)
.build <- ggplot_build(p$plot)
.build_data <- .build$data[[1]]
expect_equal(length(unique(.build_data[['group']])), 4)
expect_lt(nrow(.build_data[(.build_data[['group']] >= 3) &
(.build_data[['x']] < start_time), ]), 3)
})

test_that("survplot_combine includes dataframes when keep.data==TRUE", {
p <- ggsurvplot_combine(list(
original=fit1, conditional=fit2
), data = lung, keep.data = TRUE)
expect_equal(length(names(p)), 3)
})
21 changes: 21 additions & 0 deletions tests/testthat/test-ggsurvplot_facet.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_that("ggsurvplot_facet creates the correct quanitty of subplots", {
fit <- survfit(Surv(time, status) ~ sex, data=kidney)
p <- ggsurvplot_facet(fit, kidney, facet.by='disease')
.build <- ggplot_build(p)
expect_equal(nrow(.build$data[[2]]),
length(unique(kidney[['disease']])))

fit <- survfit(Surv(time, status) ~ disease, data=kidney)
p <- ggsurvplot_facet(fit, kidney, facet.by='sex')
.build <- ggplot_build(p)
expect_equal(nrow(.build$data[[2]]),
length(unique(kidney[['sex']])))
})

test_that("ggsurvplot_facet calculates pvalue for each facet", {
fit <- survfit(Surv(time, status) ~ sex, data=kidney)
p <- ggsurvplot_facet(fit, kidney, facet.by='disease', pval = TRUE)
.build <- ggplot_build(p)
expect_equal(nrow(.build$plot$layer[[4]][['data']]),
length(unique(kidney[['disease']])))
})
9 changes: 5 additions & 4 deletions tests/testthat/test-ggsurvtable.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
context("test-ggsurvtable")

library(dplyr)
library("survival")
data("lung")
fit <- survfit(Surv(time, status) ~ sex, data = lung)


test_that("survtable y axis label colors work", {
library(dplyr)
library("survival")
#data("lung", package = "survival")
fit <- survfit(Surv(time, status) ~ sex, data = lung)
p <- ggrisktable(fit, data = lung, color = "strata")
.build <- ggplot_build(p)
.build_data <- .build$data[[1]]
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-surv_median.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test_that("surv_median calculates medians", {
df_medians <- surv_median(survfit(Surv(time, status) ~ sex,
data=lung))
expect_equal(df_medians[['median']], c(270,426))
expect_lt(df_medians[['lower']][2], 426)
})
Loading