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!

New contributor

IsaFleurvG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

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))

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

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

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

Could you adapt this to your use-case?

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

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!

New contributor

IsaFleurvG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

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))

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

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

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

Could you adapt this to your use-case?

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

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!

New contributor

IsaFleurvG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

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))

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

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

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

Could you adapt this to your use-case?

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật