R - 使用lines()在曲线上添加一行不起作用 - 为什么?

时间:2016-03-01 16:56:22

标签: r plot line graphic

我试图在图表上放两行。数据来自数值变量表:

> 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"))

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

DoesntWork

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

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)

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

Doesn'tWork2

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

1 个答案:

答案 0 :(得分:1)

您在lines()的通话中没有提供x值;尝试:

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