Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set X-axis Limits in plot.adjustedsurv() from the AdjustedSurv Package in R?

I’m using the AdjustedSurv package in R to create adjusted survival curves with the IPTW_cox method. However, I’m running into an issue with setting the x-axis limits for the plot.

This is the code that I'm using:

# Apply g-computation with confidence intervals for adjusted survival curves
adjsurv_selected <- adjustedsurv(data = df_selected,
                                 variable = "AAA_size",          
                                 ev_time = "Surv_years",         
                                 event = "DEAD",                
                                 method = "iptw_cox",
                                 treatment_model = glm_mod)

# Plot the adjusted survival curves
p1 <- plot(adjsurv_selected, 
           conf_int = FALSE,
           xlab = "Years",
           ylab = "Adjusted Survival Probability",
           risk_table = TRUE,
           risk_table_stratify = TRUE,
           risk_table_title = "AAA size",
           risk_table_ylab = "AAA size",
           title = "Adjusted Survival Curve Stratified by AAA Size",
           legend.labs = levels(df_selected$AAA_size),
           legend.title = "AAA size",
           xlim = c(0,5),   # This doesn't seem to work
           ylim = c(0, 1))

set.seed(42)  # For reproducibility

# Create a minimal dataset
data <- data.frame(
  AAA_size = factor(rep(c("Medium", "Large"), each = 5)),
  AGE = sample(50:80, 10, replace = TRUE),
  GENDER = factor(rep(c("Male", "Female"), each = 5)),
  renal_dysf = sample(c(0, 1), 10, replace = TRUE),
  prior_chf_log = sample(c(0, 1), 10, replace = TRUE),
  DIABETES = sample(c(0, 1), 10, replace = TRUE),
  prior_hypertension = sample(c(0, 1), 10, replace = TRUE),
  prior_copd = sample(c(0, 1), 10, replace = TRUE),
  bmi_cat = sample(c("Normal", "Overweight", "Obese"), 10, replace = TRUE),
  prior_smoking = sample(c(0, 1), 10, replace = TRUE),
  prior_mi = sample(c(0, 1), 10, replace = TRUE),
  preop_anemia = sample(c(0, 1), 10, replace = TRUE),
  branches = sample(1:3, 10, replace = TRUE),
  prior_asause = sample(c(0, 1), 10, replace = TRUE),
  prior_p2y = sample(c(0, 1), 10, replace = TRUE),
  prior_statinuse = sample(c(0, 1), 10, replace = TRUE),
  physvol = sample(c(0, 1), 10, replace = TRUE),
  Surv_years = runif(10, 0, 5),
  DEAD = sample(c(0, 1), 10, replace = TRUE)
)

# Output the dataset with dput
dput(head(data))

# Load necessary libraries
library(survival)
library(survey)

# Fit a logistic regression model
glm_mod <- glm(AAA_size ~ AGE + GENDER + renal_dysf +
                 prior_chf_log + DIABETES + prior_hypertension +
                 prior_copd + bmi_cat + prior_smoking +
                 prior_mi + preop_anemia + branches + prior_asause +
                 prior_p2y + prior_statinuse + physvol, 
               data = data, family = "binomial")

# Apply g-computation with confidence intervals for adjusted survival curves
adjsurv_selected <- adjustedsurv(data = data,
                                 variable = "AAA_size",
                                 ev_time = "Surv_years",
                                 event = "DEAD",
                                 method = "iptw_cox",
                                 treatment_model = glm_mod)

# Plot the adjusted survival curves
p1 <- plot(adjsurv_selected, 
           conf_int = FALSE,
           xlab = "Years",
           ylab = "Adjusted Survival Probability",
           risk_table = TRUE,
           risk_table_stratify = TRUE,
           risk_table_title = "AAA size",
           risk_table_ylab = "AAA size",
           title = "Adjusted Survival Curve Stratified by AAA Size",
           legend.labs = levels(data$AAA_size),
           legend.title = "AAA size",
           xlim = c(0,5),
           ylim = c(0, 1))


I noticed that while there is an argument for ylim, there doesn’t seem to be a similar argument for xlim. It appears that the x-axis is automatically set to the longest observed time in my dataset.

Despite specifying xlim = c(0, 5), I’m unable to adjust the x-axis limits, which are automatically set based on the longest observed time in my data.

Is there a way to manually set the x-axis limits in the plot.adjustedsurv() function or work around this limitation? Any guidance or suggestions would be greatly appreciated.

I want to set the X-lim to 5 years.

Thanks in advance!

like image 544
IsaFleurvG Avatar asked Oct 29 '25 20:10

IsaFleurvG


1 Answers

Edit

TL;DR complicated problem with a simple solution:

additional_layers = list(xlim(c(0, 5)))

Original answer

The issue is caused by the risk.table options, e.g.

Load data and create a model:

data <- data.frame(
  AAA_size = factor(rep(c("Medium", "Large"), each = 5)),
  AGE = sample(50:80, 10, replace = TRUE),
  GENDER = factor(rep(c("Male", "Female"), each = 5)),
  renal_dysf = sample(c(0, 1), 10, replace = TRUE),
  prior_chf_log = sample(c(0, 1), 10, replace = TRUE),
  DIABETES = sample(c(0, 1), 10, replace = TRUE),
  prior_hypertension = sample(c(0, 1), 10, replace = TRUE),
  prior_copd = sample(c(0, 1), 10, replace = TRUE),
  bmi_cat = sample(c("Normal", "Overweight", "Obese"), 10, replace = TRUE),
  prior_smoking = sample(c(0, 1), 10, replace = TRUE),
  prior_mi = sample(c(0, 1), 10, replace = TRUE),
  preop_anemia = sample(c(0, 1), 10, replace = TRUE),
  branches = sample(1:3, 10, replace = TRUE),
  prior_asause = sample(c(0, 1), 10, replace = TRUE),
  prior_p2y = sample(c(0, 1), 10, replace = TRUE),
  prior_statinuse = sample(c(0, 1), 10, replace = TRUE),
  physvol = sample(c(0, 1), 10, replace = TRUE),
  Surv_years = runif(10, 0, 5),
  DEAD = sample(c(0, 1), 10, replace = TRUE)
)

# Load necessary libraries
library(ggplot2)
library(survival)
library(survey)
library(adjustedCurves)

# Fit a logistic regression model
glm_mod <- glm(AAA_size ~ AGE + GENDER + renal_dysf +
                 prior_chf_log + DIABETES + prior_hypertension +
                 prior_copd + bmi_cat + prior_smoking +
                 prior_mi + preop_anemia + branches + prior_asause +
                 prior_p2y + prior_statinuse + physvol, 
               data = data, family = "binomial")

# Apply g-computation with confidence intervals for adjusted survival curves
adjsurv_selected <- adjustedsurv(data = data,
                                 variable = "AAA_size",
                                 ev_time = "Surv_years",
                                 event = "DEAD",
                                 method = "iptw_cox",
                                 treatment_model = glm_mod)
#> Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
#> prediction from rank-deficient fit; attr(*, "non-estim") has doubtful cases

Without the risk table (x limit 2 years):

# Plot the adjusted survival curves
plot(adjsurv_selected, 
           conf_int = FALSE,
           xlab = "Years",
           ylab = "Adjusted Survival Probability",
           #risk_table = TRUE,
           #risk_table_stratify = TRUE,
           #risk_table_title = "AAA size",
           #risk_table_ylab = "AAA size",
           title = "Adjusted Survival Curve Stratified by AAA Size",
           legend.labs = levels(data$AAA_size),
           legend.title = "AAA size",
           #xlim = c(0,5),
           #ylim = c(0, 1)
) +
  coord_cartesian(x = c(0, 2))

survival curves with x limits

With the risk table (x limits ignored):

# Plot the adjusted survival curves
p1 <- plot(adjsurv_selected, 
     conf_int = FALSE,
     xlab = "Years",
     ylab = "Adjusted Survival Probability",
     risk_table = TRUE,
     risk_table_stratify = TRUE,
     risk_table_title = "AAA size",
     risk_table_ylab = "AAA size",
     title = "Adjusted Survival Curve Stratified by AAA Size",
     legend.labs = levels(data$AAA_size),
     legend.title = "AAA size"
) +
  scale_x_continuous(limits = c(0,2))
#> Loading required namespace: cowplot
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.
p1

survival curves x limit ignored

Created on 2024-09-18 with reprex v2.1.0


A potential solution is to change the limits 'manually', e.g.

# Plot the adjusted survival curves
p1 <- plot(adjsurv_selected, 
     conf_int = FALSE,
     xlab = "Years",
     ylab = "Adjusted Survival Probability",
     risk_table = TRUE,
     risk_table_stratify = TRUE,
     risk_table_title = "AAA size",
     risk_table_ylab = "AAA size",
     title = "Adjusted Survival Curve Stratified by AAA Size",
     legend.labs = levels(data$AAA_size),
     legend.title = "AAA size"
)
#> Loading required namespace: cowplot
p1$coordinates$limits$x <- c(0, 0.6)
p1

survival curves with risk table and x limits

Created on 2024-09-18 with reprex v2.1.0

Could you adapt this to your use-case?

like image 138
jared_mamrot Avatar answered Oct 31 '25 12:10

jared_mamrot