简体   繁体   English

如何从R图导出X和Y图值?

[英]How to export X and Y plot values from an R plot?

Using the eha and survival packages in R, I am using the following code to create the survival plot below: 使用R中的eha和生存包,我使用以下代码创建以下生存图:

with(dataset, plot(Surv(enter, exit, event), ylim= c(0.87, 1.0), fn = "surv", strata = Gender))

在此处输入图片说明

Rather than exporting the image of the plot, how can I export the X and Y values for each line on this plot? 除了导出图的图像,我如何导出该图上每条线的X和Y值? I attempted to use the following code, however it only creates a blank .csv file: 我试图使用以下代码,但是它只会创建一个空白的.csv文件:

write.table((with(dataset, plot(Surv(enter, exit, event), ylim= c(0.87, 1.0), fn = "surv", strata = Gender))), file="testfit.csv", sep=',')

Thanks for any help in advance. 感谢您的任何帮助。

On a secondary note, does anyone happen to know how to make this plot look nicer in ggplot2? 在第二点上,有没有人碰巧知道如何使该图在ggplot2中看起来更好?

You can use the gridGraphics package to convert your plot to a grob, then grab the points from out of the grob object. 您可以使用gridGraphics包将绘图转换为grob,然后从grob对象中获取点。 I like this solution because it is applicable to a wide range of problems. 我喜欢此解决方案,因为它适用于各种问题。

Using the graph that was created here for an example graph: 使用此处创建的图形作为示例图形:

# plot a graph
library(survival)
library(grid)
library(gridGraphics)
data(lung)
lung.surv <- survfit(Surv(time,status) ~ 1, data = lung)
plot(lung.surv)

# capture the plotted output as a grob
grid.echo()
grid.grab() -> k

# pull out the data from the grob..
k$children$`graphics-plot-1-points-1`$x -> x
k$children$`graphics-plot-1-points-1`$y -> y

I noticed that your plot is stratified by gender...Make sure you pull out the correct subset of the points for each gender. 我注意到您的情节按性别分层...请确保为每种性别提取正确的点子集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM