R - 使用 lines() 向绘图添加一条线不起作用 - 为什么?

R - Adding a line to a plot with lines() does not work - why?

我想在图表上画两条线。数据来自 table 个数值变量:

> str(tab1)
'data.frame':   101 obs. of  5 variables:
 $ Cut_Point: num  -4.63 -2.85 -1.92 -1.86 -1.73 ...
 $ N_Samples: int  63 63 63 63 63 63 63 63 63 63 ...
 $ Wilcoxon : num  0.0382 0.0382 0.0382 0.0382 0.0382 ...
 $ Cox_PH   : num  0.0571 0.0571 0.0571 0.0572 0.0572 ...

我认为这会很简单,所以我编写了以下代码:

plot(tab1$Cut_Point, -log10(tab1$Wilcoxon), type = "l", col = "red", main = "P-values vs Score", xlab = "Log10(Score)", ylab = "-Log10(P-Value)", ylim = c(0.5,1.5), xlim = c(-2,0.3))
coxline = -log10(tab1$Cox_PH)
lines(coxline, col = "blue")
abline(a = 1.122018, b = 0, col = "black")
legend("bottomright", c("Wilcoxon", "Cox PH", "P = 0.05"), lty = c(1,1,1),col = c("red", "blue", "black"))

生成以下缺少蓝线的图形:

两个变量的取值范围不是问题,如下代码:

par(mfrow = c(2,1))
plot(tab1$Cut_Point, -log10(tab1$Cox_PH), type = "l", main = "Cox PH P-values vs Score", xlab = "Log10(Score)", ylab = "-Log(P-Value)", ylim = c(1, 1.5), xlim = c(-2,0.3))
abline(a = 1.122018, b = 0)
plot(tab1$Cut_Point, -log10(tab1$Wilcoxon), type = "l", main = "Wilcoxon P-values vs Score", xlab = "Log10(Score)", ylab = "-Log10(P-Value)", ylim = c(1,1.5), xlim = c(-2,0.3))
abline(a = 1.122018, b = 0)

生成以下图形;请注意,顶部图形的线条定义与第一个图形中未出现的线条完全相同:

我曾尝试删除 plot() 命令中的 xlim()ylim() 参数,也曾尝试使用 tab1$log_cox = -log10(tab1$Cox_PH) 创建一个新列,但这些都没有方法使难以捉摸的第二条蓝线出现。 None 我的代码会产生错误消息,所以我真的不知道为什么第二行没有出现。欢迎使用包含两条线的替代方法生成图表 但我真正想知道和理解的是为什么我的代码不生成蓝线?

您在调用 lines() 时没有提供 x 值;尝试:

lines(tab1$Cut_Point , coxline, col = "blue")