处理特殊 class (stdCoxph) 数据时更改基本绘图函数 R 的颜色
Change color in basic plot function R when working with data of special class (stdCoxph)
我通常使用 ggplot 进行所有绘图,但现在我使用名为 stdCoxph 的程序包处理标准化生存曲线。我的问题是:有谁知道如何改变这些线条的颜色和粗细。我试过使用 col 和 lwd,但它没有改变任何东西。请参阅下面的可重现示例:
#Load packages:
if(!require(stdReg)){install.packages("stdReg", dependencies=TRUE)}
library(stdReg)
library(survival)
n <- 1000
Z <- rnorm(n)
X <- rnorm(n, mean=Z)
Tm <- rexp(n, rate=exp(X+Z+X*Z)) #survival time
C <- rexp(n, rate=exp(X+Z+X*Z)) #censoring time
U <- pmin(Tm, C) #time at risk
D <- as.numeric(Tm < C) #event indicator
dd <- data.frame(Z, X, U, D)
fit <- coxph(formula=Surv(U, D)~ X*Z, data=dd, method="breslow")
# in R formulas ~X*C will be interpreted as ~X+Z+X:Z
fit.std <- stdCoxph(fit=fit, data=dd, X="X", x=seq(-1,1,0.5), t=1:5)
print(summary(fit.std, t=3))
plot(fit.std)
col
是硬编码的,但可以使用 par
修改其他绘图参数:
opar <- par(lwd = 3)
plot(std.fit)
par(opar) # reset back
要更改颜色,请重新定义 plot.stdCoxph
添加颜色参数。这会创建 plot.stdCoxph
的副本,它调用使用指定颜色的 lines
和 legend
的本地副本。
plot.stdCoxph <- function(x, ..., colors = seq_along(x)) {
lines <- function(x, ..., col) graphics::lines(x, ..., col = colors[col])
legend <- function(x, ..., col) graphics::legend(x, ..., col = colors[col])
plot.stdCoxph <- stdReg:::plot.stdCoxph
environment(plot.stdCoxph) <- environment()
plot.stdCoxph(x, ...)
}
plot(fit.std, colors = rainbow(5)) # test
您可以考虑联系软件包的维护者,询问他们是否可以在没有此解决方法的情况下提供类似的功能。
我通常使用 ggplot 进行所有绘图,但现在我使用名为 stdCoxph 的程序包处理标准化生存曲线。我的问题是:有谁知道如何改变这些线条的颜色和粗细。我试过使用 col 和 lwd,但它没有改变任何东西。请参阅下面的可重现示例:
#Load packages:
if(!require(stdReg)){install.packages("stdReg", dependencies=TRUE)}
library(stdReg)
library(survival)
n <- 1000
Z <- rnorm(n)
X <- rnorm(n, mean=Z)
Tm <- rexp(n, rate=exp(X+Z+X*Z)) #survival time
C <- rexp(n, rate=exp(X+Z+X*Z)) #censoring time
U <- pmin(Tm, C) #time at risk
D <- as.numeric(Tm < C) #event indicator
dd <- data.frame(Z, X, U, D)
fit <- coxph(formula=Surv(U, D)~ X*Z, data=dd, method="breslow")
# in R formulas ~X*C will be interpreted as ~X+Z+X:Z
fit.std <- stdCoxph(fit=fit, data=dd, X="X", x=seq(-1,1,0.5), t=1:5)
print(summary(fit.std, t=3))
plot(fit.std)
col
是硬编码的,但可以使用 par
修改其他绘图参数:
opar <- par(lwd = 3)
plot(std.fit)
par(opar) # reset back
要更改颜色,请重新定义 plot.stdCoxph
添加颜色参数。这会创建 plot.stdCoxph
的副本,它调用使用指定颜色的 lines
和 legend
的本地副本。
plot.stdCoxph <- function(x, ..., colors = seq_along(x)) {
lines <- function(x, ..., col) graphics::lines(x, ..., col = colors[col])
legend <- function(x, ..., col) graphics::legend(x, ..., col = colors[col])
plot.stdCoxph <- stdReg:::plot.stdCoxph
environment(plot.stdCoxph) <- environment()
plot.stdCoxph(x, ...)
}
plot(fit.std, colors = rainbow(5)) # test
您可以考虑联系软件包的维护者,询问他们是否可以在没有此解决方法的情况下提供类似的功能。