问题
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