我可以将R中的基本图转换为ggplot对象吗? [英] can i convert a base plot in r to a ggplot object?

查看:465
本文介绍了我可以将R中的基本图转换为ggplot对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R有点陌生,我喜欢ggplot-这是我用于绘图的全部功能,所以我不知道R中基本绘图所需的所有古式语法(我宁愿不学习它).我正在运行pROC :: roc,我想在ggplot中绘制输出(以便我可以微调它的外观).我可以立即得到如下图:

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)

要改为使用ggplot,我可以从输出创建一个数据帧,然后使用ggplot(这不是我的问题).我想知道的是,是否可以通过某种方式将上面代码中的绘图自动转换"为ggplot,以便可以在ggplot中完成所需的工作?我到处搜索,似乎找不到这个基本"问题的答案.谢谢!

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!!

推荐答案

不,我认为这是不可能的.

No, I think unfortunately this is not possible.

即使这不能回答您的真正问题,使用ggplot构建它实际上也不难.

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

您的原始剧情:

plot(rocobject)

在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")

这篇关于我可以将R中的基本图转换为ggplot对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆