Unable to save KM plot using ggsave in R

I am trying to save a KM plot using ggsave but my following code isn't saving the file on my Mac:

# Fit survival curves
fit<- survfit(Surv(time, status) ~ sex, data = lung)
# Visualize
p <- ggsurvplot(fit, data = lung,
                surv.median.line = "hv", # Add medians survival
                pval = TRUE,             # Add p-value and tervals
                conf.int = TRUE,        # Add the 95% confidence band
                risk.table = TRUE,      # Add risk table
                tables.height = 0.2,
                tables.theme = theme_cleantable(),
                palette = "jco",
                ggtheme = theme_bw()

ggsave(filename = "Test.png",
       plot = print(p),
       path = "~/Desktop/",
       width = 8,
       height = 7,
       dpi =  300)

The code runs properly without any errors but and I can see the sample KM curve appear in R. However, no saved image file appears on my desktop. I would really appreciate any help with this request.

You might need the actual path. This worked for me.

ggsave(filename = "Test.png",
       plot = print(p),
       path = r"(C:\Users\username\Desktop)",  # edit
       width = 8,
       height = 7,
       dpi =  300)

How to get the real path of Desktop in Windows Explorer? - Super User

Thanks for the suggestion. I was able to generate a file but the image appears blank (although the image does appear in the Plot section in R). Any suggestions on what might be causing this issue?

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.