llarsson3
llarsson3

Reputation: 1

Is there a way to save a plot generated with pROC as .svg or .ai?

I have to submit figures generated with pROC package to a journal that requires editable figures (in .svg or .ai formatting specifically). Until now, I have just been right clicking on the figure as it is generated and saving it as .png. Unfortunately, this will not do for the journal. Here is my code:

library(pROC)

par(pty="s")

r_lda = roc(crs$CRS, crs$tbscore_corr_lda_inv_crs)
r_cf1 = roc(crs$CRS, crs$cf1_inv)
r_cf3 = roc(crs$CRS, crs$cf3_inv)
r_gbp5 = roc(crs$CRS, crs$gbp5_inv)
r_dusp3 = roc(crs$CRS, crs$dusp3_inv)

multiple <- plot.roc(r_lda, print.auc=TRUE, auc.polygon=TRUE, auc.polygon.col = "#FFEBEE",print.thres=FALSE,
                     col=pal_pro[1], print.auc.y=0.50, print.auc.x=0.40, xlim = c(1.1,0))
multiple <- plot.roc(r_cf1, print.auc=TRUE, auc.polygon=FALSE,col=pal_pro[2], add=TRUE, print.auc.y=0.40, print.auc.x=0.40, xlim = c(1.2,0), ylim = c(0,1.2))
multiple <- plot.roc(r_gbp5, print.auc=TRUE, auc.polygon=FALSE,col=pal_pro[3], add=TRUE, print.auc.y=0.30, print.auc.x=0.40, xlim = c(1.2,0), ylim = c(0,1.2))
multiple <- plot.roc(r_dusp3, print.auc=TRUE, auc.polygon=FALSE,col=pal_pro[4], add=TRUE, print.auc.y=0.20, print.auc.x=0.40, xlim = c(1.2,0), ylim = c(0,1.2))

legend("bottom",
       legend=c("TB score", "KLF2", "GBP5", "DUSP3"),
       col=pal_pro,
       lwd=6, cex =0.5, xpd = TRUE, horiz = TRUE)
text(0.2, 0.65, "TB score",col=pal_pro[1], font=2)
text(0.5, 0.45, "KLF2",col=pal_pro[2])
text(0.8, 0.7, "GBP5",col=pal_pro[3])
text(0.55, 0.9, "DUSP3",col=pal_pro[4])

Would you be able to provide some advice on how to save this in an editable format?

I have tried using ggsave() with device = "svg" specified but it returns the following error:

"Error in UseMethod("grid.draw") :
no applicable method for 'grid.draw' applied to an object of class "roc""

Other than that, other solutions required me changing the plot itself which at this point I am unable to do.

Upvotes: 0

Views: 269

Answers (2)

Calimo
Calimo

Reputation: 7969

The ggsave function can save a ggplot, but you created a normal graphics plot. For that you'll need to use the functionalities of grDevices, for instance copying the plot with dev.copy()

[plot, text, legend, etc]
dev.copy(svg, "myplot.svg") # Creates a SVG device
dev.off()  # Closes the SVG device and saves the file

Or simply opening the SVG device before plotting:

svg("myplot.svg")
[plot, text, legend, etc]
dev.off()

Upvotes: 0

George Savva
George Savva

Reputation: 5336

Use the svglite package to write your plot to an svg file. Eg:

# Make some toy data
dat1 <- data.frame(x=rep(c(0,1),10), y=rnorm(20))

# Estimate a ROC
roc1 <- roc(dat1$x,dat1$y)

# Start the SVG device
svglite::svglite("rocTest.svg")

# Plot and add a text label
plot(roc1)
text(0.5,0.5,"Hi!")

# Close the device
dev.off()

Note svglite creates much better editable svg files than the regular R svg device. If you compare the files created with each you'll see the difference.

Upvotes: 1

Related Questions