R 中的 plot 函数在不调用 legend() 的情况下生成图例
plot function in R producing legend without legend() being called
我正在尝试使用 R 中的 plot() 为竞争性危害生存分析生成累积发生率图。出于某种原因,生成的图有一个我没有调用的图例。图例与我图表上的线条相交,我不知道如何摆脱它。请帮忙!
我的代码如下:
CompRisk2 <- cuminc(ftime=ADI$time_DeathTxCensor, fstatus=ADI$status, group=ADI$natADI_quart)
cols <- c("darkorange","coral1","firebrick1","firebrick4","lightskyblue","darkturquoise","dodgerblue","dodgerblue4")
par(bg="white")
plot(CompRisk2,
col=cols,
xlab="Years",
ylab="Probability of Mortality or Transplant",
xlim=c(0,10),
ylim=c(0,0.6))
产生以下情节:
我尝试添加以下代码将图例移出框架,但出现错误:
legend(0,5, legend=c(11,21,31,41,12,22,32,42),
col=c("darkorange","coral1","firebrick1","firebrick4","lightskyblue","darkturquoise","dodgerblue","dodgerblue4"),
lty=1:2, cex=0.8, text.font=4, box.lty=0)
错误:标题错误(...):图形参数无效
如有任何帮助,我们将不胜感激!
您正在使用 cmprsk
包中的 cuminc
函数。这会产生一个 class cuminc
的对象,它有一个 S3 绘图方法。 ?plot.cuminc
显示文档并输入 plot.cuminc
显示代码。
有一些稍微模糊的代码建议解决方法:
u <- list(...)
if (length(u) > 0) {
i <- pmatch(names(u), names(formals(legend)), 0)
do.call("legend", c(list(x = wh[1], y = wh[2], legend = curvlab,
col = color, lty = lty, lwd = lwd, bty = "n", bg = -999999),
u[i > 0]))
}
这表示在 ...
中传递的任何其他参数,其名称与 legend
的参数名称匹配,将传递给 legend()
. legend()
有一个 plot
参数:
plot: logical. If ‘FALSE’, nothing is plotted but the sizes are returned.
所以看起来将 plot=FALSE
添加到您的 plot()
命令会起作用。
原则上,您可以尝试查看 legend()
的其他参数,看看它们是否会根据需要调整图例 position/size。不幸的是,图例的 x
参数(将确定水平位置)被 plot.cuminc
.
的第一个参数掩盖
我认为省略号参数不适用于 plot.cuminc
内的 legend
调用。 Ben 的回答中提供的代码表明可能有一个 wh
参数来确定图例的位置。在他提供的代码中,它没有在参数中命名为“x”,而是作为位置定义的参数给出的。如果您查看 plot.cuminc
函数,您实际上会发现 wh
已记录在案。
我无法对此进行测试,因为您没有向我们提供访问 ADI
-对象的权限,但我的建议是尝试:
opar <- par(xpd=TRUE) # xpd lets graphics be placed 'outside'
plot(CompRisk2,
col=cols, wh=c(-.5, 7),
xlab="Years",
ylab="Probability of Mortality or Transplant",
xlim=c(0,10),
ylim=c(0,0.6))
par(opar) # restores original graphics parameters
未经测试就发布代码块总是有点冒险,但我很高兴地报告说我确实找到了合适的测试并且它似乎按预期合理工作。在 SO 问题 :
的对象上使用下面的代码
library(cmprsk)
# some simulated data to get started
comp.risk.data <- data.frame("tfs.days" = rweibull(n = 100, shape = 1, scale = 1)*100,
"status.tfs" = c(sample(c(0,1,1,1,1,2), size=50, replace=T)),
"Typing" = sample(c("A","B","C","D"), size=50, replace=T))
# fitting a competing risks model
CR <- cuminc(ftime = comp.risk.data$tfs.days,
fstatus = comp.risk.data$status.tfs,
cencode = 0,
group = comp.risk.data$Typing)
opar <- par(xpd=TRUE) # xpd lets graphics be placed 'outside'
plot(CR,
wh=c(-15, 1.1), # obviously different than the OP's coordinates
xlab="Years",
ylab="Probability of Mortality or Transplant",
xlim=c(0,400),
ylim=c(0,1))
par(opar) # restores graphics parameters
我让图例从原来的位置向上和向左移动。
我正在尝试使用 R 中的 plot() 为竞争性危害生存分析生成累积发生率图。出于某种原因,生成的图有一个我没有调用的图例。图例与我图表上的线条相交,我不知道如何摆脱它。请帮忙!
我的代码如下:
CompRisk2 <- cuminc(ftime=ADI$time_DeathTxCensor, fstatus=ADI$status, group=ADI$natADI_quart)
cols <- c("darkorange","coral1","firebrick1","firebrick4","lightskyblue","darkturquoise","dodgerblue","dodgerblue4")
par(bg="white")
plot(CompRisk2,
col=cols,
xlab="Years",
ylab="Probability of Mortality or Transplant",
xlim=c(0,10),
ylim=c(0,0.6))
产生以下情节:
我尝试添加以下代码将图例移出框架,但出现错误:
legend(0,5, legend=c(11,21,31,41,12,22,32,42),
col=c("darkorange","coral1","firebrick1","firebrick4","lightskyblue","darkturquoise","dodgerblue","dodgerblue4"),
lty=1:2, cex=0.8, text.font=4, box.lty=0)
错误:标题错误(...):图形参数无效
如有任何帮助,我们将不胜感激!
您正在使用 cmprsk
包中的 cuminc
函数。这会产生一个 class cuminc
的对象,它有一个 S3 绘图方法。 ?plot.cuminc
显示文档并输入 plot.cuminc
显示代码。
有一些稍微模糊的代码建议解决方法:
u <- list(...)
if (length(u) > 0) {
i <- pmatch(names(u), names(formals(legend)), 0)
do.call("legend", c(list(x = wh[1], y = wh[2], legend = curvlab,
col = color, lty = lty, lwd = lwd, bty = "n", bg = -999999),
u[i > 0]))
}
这表示在 ...
中传递的任何其他参数,其名称与 legend
的参数名称匹配,将传递给 legend()
. legend()
有一个 plot
参数:
plot: logical. If ‘FALSE’, nothing is plotted but the sizes are returned.
所以看起来将 plot=FALSE
添加到您的 plot()
命令会起作用。
原则上,您可以尝试查看 legend()
的其他参数,看看它们是否会根据需要调整图例 position/size。不幸的是,图例的 x
参数(将确定水平位置)被 plot.cuminc
.
我认为省略号参数不适用于 plot.cuminc
内的 legend
调用。 Ben 的回答中提供的代码表明可能有一个 wh
参数来确定图例的位置。在他提供的代码中,它没有在参数中命名为“x”,而是作为位置定义的参数给出的。如果您查看 plot.cuminc
函数,您实际上会发现 wh
已记录在案。
我无法对此进行测试,因为您没有向我们提供访问 ADI
-对象的权限,但我的建议是尝试:
opar <- par(xpd=TRUE) # xpd lets graphics be placed 'outside'
plot(CompRisk2,
col=cols, wh=c(-.5, 7),
xlab="Years",
ylab="Probability of Mortality or Transplant",
xlim=c(0,10),
ylim=c(0,0.6))
par(opar) # restores original graphics parameters
未经测试就发布代码块总是有点冒险,但我很高兴地报告说我确实找到了合适的测试并且它似乎按预期合理工作。在 SO 问题
library(cmprsk)
# some simulated data to get started
comp.risk.data <- data.frame("tfs.days" = rweibull(n = 100, shape = 1, scale = 1)*100,
"status.tfs" = c(sample(c(0,1,1,1,1,2), size=50, replace=T)),
"Typing" = sample(c("A","B","C","D"), size=50, replace=T))
# fitting a competing risks model
CR <- cuminc(ftime = comp.risk.data$tfs.days,
fstatus = comp.risk.data$status.tfs,
cencode = 0,
group = comp.risk.data$Typing)
opar <- par(xpd=TRUE) # xpd lets graphics be placed 'outside'
plot(CR,
wh=c(-15, 1.1), # obviously different than the OP's coordinates
xlab="Years",
ylab="Probability of Mortality or Transplant",
xlim=c(0,400),
ylim=c(0,1))
par(opar) # restores graphics parameters
我让图例从原来的位置向上和向左移动。