How can I plot a step graph in R?
0
0
Entering edit mode
5 weeks ago

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 • 717 views
ADD COMMENT
1
Entering edit mode

Please, show us what you have tried so far to reach your goal and we can see how to improve it. The community is only here to provide guidance and submit inputs.

https://stackoverflow.com/questions/23463698/how-to-create-a-stair-step-plot-of-data-in-r-ggplot2

https://stats.stackexchange.com/questions/14620/how-to-plot-a-stair-steps-function-with-ggplot

ADD REPLY
0
Entering edit mode

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
ADD REPLY
0
Entering edit mode

What is the problem with this plot ?

ADD REPLY
0
Entering edit mode
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.

ADD REPLY
0
Entering edit mode

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.

ADD REPLY

Login before adding your answer.

Traffic: 1800 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6