plot.new has not been called yet when making tiff-file

一世执手 提交于 2019-12-12 05:16:10

问题


I am trying to add text (the nonlinearity p-value) to a plot of a restricted cubic spline regression model. When I try this in R studio it works fine, however the moment I try to add code to save it as a .tiff file, I get the following:

Error in strwidth(legend, units = "user", cex = cex, font = text.font) :
plot.new has not been called yet

This is my code (for a restricted cubic spline regression model for the association between determinant labtsh and endpoint ecard with 3 knots):

spline <- cph(Surv(ecard_f,ecard_n)~rcs(labtsh,3)+age+gender+ckdepi+smoking, data=smart)

tiff(file ="H:/documents/test.tiff", width = 1000, height =1000, units = "px", res = 145) 

plot(Predict(spline,labtsh,fun=exp), pch = 20, las = 1,
    conf.int=T, 
    main="Relationship TSH and myocardial infarction", 
    xlab="TSH (mIU/L)", 
    ylab="Hazard Ratio")

my.p <- anova(spline)[2,3]  ##this is the value I want to add in the plot

rp = vector('expression',2)
rp[1] = substitute(expression(Non-linearity))[2]
rp[2] = substitute(expression(italic(p)-value == MYVALUE), 
               list(MYVALUE = format(my.p, digits = 3)))[2]
legend('topleft', legend = rp, bty = 'n')

dev.off()

Like I said, if I take away the call to make it a .tiff-file and the dev.off() line, the code works fine.

I have searched multiple answers for similar questions here on stackoverflow, but the answers don't seem to help me.

来源:https://stackoverflow.com/questions/43611387/plot-new-has-not-been-called-yet-when-making-tiff-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!