I need to create a plot that compares interval censored survival curves for three species. I am able to generate a plot that shows all three curves using the ic_np
function in the icenReg
package in R. When I plot the output of this ic_np
fit using base R plot()
, a legend appears in the bottom left corner.
This example from the icenReg
package documentation yields a similar figure:
library(icenReg)
data(miceData)
fit <- ic_np(cbind(l, u) ~ grp, data = miceData) #Stratifies fit by group
plot(fit)
However, having the caption in the bottom left covers the most interesting comparison of my survival curves, so I would like to move the legend to the top right.
I have seen this question about setting a legend position for basic plots in base R. Answers to this question seem to assume that I can generate a plot without the legend, but I have not been able to do that.
I have also seen this question about adding a legend to other types of survival analysis that do not seem to generate a legend by default, but I have not been able to implement these methods with interval censored data.
I have read that I can't move a legend that has already been added to a plot, but I don't know how to generate this particular plot without a legend so that I can add one back in where I want it (top right).
How can I either (a) generate this plot of interval censored Kaplan-Meier survival curves using ic_np
without a legend -- maybe using some hidden parameter of plot()
-- OR (b) generate this figure using a different plotting device, assuming the plot legend is then moveable?
There doesn't seem to be a help page in the package for the plot function so you need to determine the class of the fit
-object and look at the code:
class(fit)
#[1] "ic_npList"
#attr(,"package")
#[1] "icenReg"
plot.ic_npList
#Error: object 'plot.ic_npList' not found
So it's not exported and we need to dig deeper (not suprising since exported functions do need to have help pages.)
getAnywhere(plot.ic_npList)
#-----------
A single object matching ‘plot.ic_npList’ was found
It was found in the following places
registered S3 method for plot from namespace icenReg
namespace:icenReg
with value
function (x, fitNames = NULL, lgdLocation = "bottomleft", ...)
{
addList <- list(xlim = x$xRange, ylim = c(0, 1), xlab = "time",
ylab = "S(t)", x = NA)
dotList <- list(...)
#.........
#..........
legend(lgdLocation, legend = grpNames, col = cols, lty = 1)
}
<bytecode: 0x7fc9784fa660>
<environment: namespace:icenReg>
So there is a location parameter for the legend placement and the obvious alternative to try is:
plot(fit, lgdLocation = "topright")