r - R 中的绘图函数在不调用 legend() 的情况下生成图例

标签 r plot legend legend-properties

我正在尝试使用 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))

这会产生以下图: enter image description here

我尝试添加以下代码将图例移出框架,但出现错误:

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 函数。这会生成一个 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() 有一个 plot 参数:

plot: logical. If ‘FALSE’, nothing is plotted but the sizes are returned.

所以看起来将 plot=FALSE 添加到 plot() 命令中将会起作用。

原则上,您可以尝试查看 legend() 的其他参数,看看它们是否会根据您的需要调整图例位置/大小。不幸的是,图例的 x 参数(将确定水平位置)被 plot.cuminc 的第一个参数掩盖。

关于r - R 中的绘图函数在不调用 legend() 的情况下生成图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67904833/

相关文章:

r - 如何为不同级别指定不同的颜色范围?

python - 图例中带有线和点标记的两个图共享相同的标签

python - 如何用 distplot 绘制两个图例

python - 图例中的分级尺寸符号

python - rpy2 + 负二项式 glm

r - 如何找到二进制文件的长度

python - 如何用不同的名称保存文件而不覆盖现有文件

java - 处理中串行端口(USB UART 接口(interface) - FTDI)的不稳定实时绘图

r - 有没有办法将 R 中的线性回归摘要导出为图像?我需要将摘要导出为 .jpeg 或 .png

python - 无法控制 df.plot() 上第二个 y 轴的比例