如何用ggsurvplot保存指定尺寸与分辨率的JPEG格式输出?
Hi Tariq, nice question! There are two simple, reliable ways to save your ggsurvplot output as a JPEG with your exact specifications (2.2 inches height, 3.3 inches width, 1000 DPI). Let me break them down for you:
Method 1: Use ggsave() (Recommended)
This is the standard, ggplot-friendly way to save plots, and it works seamlessly with ggsurvplot since it returns a ggplot-compatible object.
First, assume you've already created your survival plot object like this:
# Example code to generate your ggsurvplot object library(survival) library(survminer) fit <- survfit(Surv(time, status) ~ sex, data = lung) gg_plot <- ggsurvplot(fit, data = lung)
Then use ggsave() with your desired parameters:
ggsave( filename = "survival_curve.jpg", # Name your output file plot = gg_plot, # Pass your ggsurvplot object width = 3.3, # Width in inches height = 2.2, # Height in inches units = "in", # Specify units as inches dpi = 1000 # Set resolution to 1000 DPI )
Method 2: Use R's Base Graphics Device
If you prefer using R's base plotting utilities, you can manually open a JPEG device, print your plot, then close the device:
# Open the JPEG device with your parameters jpeg( filename = "survival_curve.jpg", width = 3.3, height = 2.2, units = "in", res = 1000 ) # Print your ggsurvplot object print(gg_plot) # Close the device to finalize the file dev.off()
A quick note: ggsurvplot itself doesn't have built-in arguments to set save dimensions/resolution directly—since it generates a ggplot-compatible object, we rely on dedicated saving functions like ggsave or base devices to handle those details. Either method above will get you exactly the output you want!
内容的提问来源于stack exchange,提问作者Tariq Nisar

