2016-03-01 74 views
-1

我想把兩條線放在一個圖上。該數據是從數字變量的表來:R - 用線條()添加一條線條不起作用 - 爲什麼?

> 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

我試圖在plot()命令擺脫的xlim()ylim()參數和有也試圖用tab1$log_cox = -log10(tab1$Cox_PH)創建一個新列,但這些方法都沒有使難以捉摸的第二條藍線出現。 我的代碼都沒有生成錯誤消息,所以我真的不知道爲什麼第二行不顯示。包含這兩行的圖形的替代方法是歡迎但我真正想知道和理解的是爲什麼我的代碼不會產生藍線?

回答

1

您在撥打lines()時未提供x值;請嘗試:

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