3
votes

I'm somewhat new to R and I love ggplot - that's all I use for plotting, so I don't know all the archaic syntax needed for base plots in R (and I'd rather not have learn it). I'm running pROC::roc and I would like to plot the output in ggplot (so I can fine tune how it looks). I can immediately get a plot as follows:

size <- 100
response <- sample(c(0,1), replace=TRUE, size=size)
predictor <- rnorm(100)
rocobject <- pROC::roc(response, predictor,smooth=T)
plot(rocobject)

To use ggplot instead, I can create a data frame from the output and then use ggplot (this is NOT my question). What I want to know is if I can somehow 'convert' the plot made in the code above into ggplot automatically so that I can then do what I want in ggplot? I've searched all over and I can't seem to find the answer to this 'basic' question. Thanks!!

2
In the methods, there is no ggplot - akrun
More or less the answer is no. ggplot2 is built on the grid graphic system, which is distinct from ggplot2. It is now possible to combine grid graphs with base graphs in a "matrix" of graphs using the gridGraphics package. - lmo
Paul murrel did some work to convert base to grid...close to what y ou want github.com/pmur002/gridgraphics - Tyler Rinker
Thanks Tyler - this looks helpful! - cfosser

2 Answers

4
votes

Better late than never? I think the ggplotify package might do what you want. You basically plug in your plot generating code to the as.ggplot() function like so:

p6 <- as.ggplot(~plot(iris$Sepal.Length, iris$Sepal.Width, col=color, pch=15))

https://cran.r-project.org/web/packages/ggplotify/vignettes/ggplotify.html

3
votes

No, I think unfortunately this is not possible.

Even though this does not answer your real question, building it with ggplot is actually not difficult.

Your original plot:

plot(rocobject)

enter image description here

In ggplot:

library(ggplot2)
df<-data.frame(y=unlist(rocobject[1]), x=unlist(rocobject[2]))
ggplot(df, aes(x, y)) + geom_line() + scale_x_reverse() + geom_abline(intercept=1, slope=1, linetype="dashed") + xlab("Specificity") + ylab("sensitivity")

enter image description here