This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How can I plot a step graph in R?

I want to plot a step graph or an inverse KM plot in R. Please help me to plot the graph with R scripts. I have attached a picture of plot which I want to make for my data exactly similar.

enter image description here

recurrence step-graph survival

I'm getting a plot like that with this code

library(openxlsx)
library(survival)
library(survminer)

data = read.xlsx("PQ_data.xlsx")

km_fit <- survfit(Surv(time, Status) ~ group, data = data)

p= ggsurvplot(km_fit, 
           data = data, 
           fun = "event",  # This specifies we want to plot the event probability (risk of recurrence)
           pval = TRUE,     # Show p-value for comparison between groups
           risk.table = TRUE, 
           conf.int = FALSE, 
           #palette = c("#E7B800", "#2E9FDF"),  # Custom colors for groups
           title = "Kaplan-Meier Curve for Risk of Recurrence",
           xlab = "Time (Weeks)", 
           ylab = "Risk of Recurrence",
           legend.title = "Group",
           #legend.labs = c("Group A", "Group B")
           )

jpeg("km_plot_risk_of_recurrence.jpg",height=15*350,width=25*350,res=800)
p
dev.off()

enter image description here

Please give inputs how can I improve this.

My input data is like so:

country        drug   dosage      group        time   Status
Afghanistan    PQ     0.25        very_low     52     1
Colombia       PQ     0.25        very_low     11     1
Vietnam        PQ     0.5         high         4      1
India          PQ     75          weekly       52     1
Brazil         PQ     0.5         high         26     1
China          PQ     0.45        low          4      1
India          PQ     75          weekly       52     1
Japan          PQ     210         weekly       12     1

What is the problem with this plot ?

library(survival)
library(survminer)
library(readxl)

# Load the data from the attached Excel file
input_data <- read_excel("oceania-pq.xlsx", sheet = 1)

# Clean and prepare the data
input_data <- input_data %>%
  dplyr::mutate(
    Dosage_Level = ifelse(dosage < median(dosage, na.rm = TRUE), "Low", "High"),
    Relapse_Status = ifelse(Status > 0, 1, 0)  # Create relapse status indicator
  )

# Create a survival object
surv_obj <- Surv(time = input_data$time, event = input_data$Relapse_Status)

# Fit the survival model stratified by dosage level
fit <- survfit(surv_obj ~ Dosage_Level, data = input_data)


# Plot the survival curves with step-up aesthetics for risk

plot <- ggsurvplot(
  fit,
  data = input_data,
  risk.table = TRUE,       # Show risk table
  conf.int = TRUE,         # Include confidence intervals
  pval = TRUE,             # Show p-value for the stratified comparison
  xlab = "Time (Weeks)",
  ylab = "Risk of Recurrence",
  title = "Risk of Relapse by Dosage Level",
  legend.title = "Dosage Level",
  legend.labs = c("Low Dosage", "High Dosage"),
  palette = c("blue", "red"),
  fun = "event"             # Plot cumulative events (step-up risk curves)
)

# Print the plot
print(plot)

I have used this R code and I don't know how to interpret the results. Can somebody please help. enter image description here

I'm getting this type of plot.

Please use ADD COMMENT/ADD REPLY when responding to existing posts. SUBMIT ANSWER should only be used for a NEW answer to the original question.

0 answers

No answers yet.

Log in to answer this question.