2017-01-31 74 views
1
lab <- data.frame(Month = c("2016-01-01", "2016-02-01", "2016-03-01", "2016-04-01", "2016-05-01", "2016-06-01"), AccNumber = c(5683,5418,6001,6184,6001,6184), OCTAT = c(40.20,50.52,47.15,45.03,47.15,45.03), Default = c(30,30,30,30,30,30)) 

id <- rownames(lab) 
lab <- cbind(id = id, lab) 
max <- max(lab$AccNumber) 
max2 <- max(lab$OCTAT) 
p <- barplot(lab$AccNumber, names.arg = lab$Month, xlab = "Month", col = "blue" ,ylim = c(0, max + 2000)) 
par(new=TRUE) 
plot(x = id, y = lab$OCTAT, type = "l", col = "red", axes = FALSE, ylim = c(0, 60), ann = FALSE) 
plot(x = id, y = lab$OCTAT, type = "l", col = "green", axes = FALSE, ylim = c(0, 60), ann = FALSE, lwd = 2) 
p <- barplot(lab$AccNumber, names.arg = lab$Month, xlab = "Month", col = "blue" ,ylim = c(0, max + 2000)) 
par(new=TRUE) 
plot(x = id, y = lab$OCTAT, type = "l", col = "green", axes = FALSE, ylim = c(0, 60), ann = FALSE, lwd = 2) 
axis(4, at=seq(0, max2 + 10, 10)) 
abline(h = 30, lwd = 2) # Default blue line (30) 
abline(lm(lab$OCTAT ~ lab$id), col = "red", lty = 2) #Regression line trend 

運行代碼後,這是我下面得到的。我對R完全陌生。我怎樣才能得到一張看起來像第二張照片的迴歸線。regresio行顯示不正確

enter image description here

enter image description here

回答

0

如果你想在紅色虛線從左邊線到右軸跨越,改變代碼的最後一行:

abline(lm(lab$OCTAT ~ as.numeric(lab$id)), col = "red", lty = 2) 

這是因爲lab$id是具有類別特徵的。

如果你想在紅色虛線跨越相同的寬度爲綠線,更改代碼的最後一行:

lines(x = c(1,6), y = c(44.9953, 46.6983), col = 'red', lty = 2) 

值44.9953和46.6983從把ID = 1和ID獲得= 6進入從lm(lab$OCTAT ~ as.numeric(lab$id)安裝的模型。

希望這是你正在尋找。