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的新手。我怎么能得到一个看起来像第二张照片的回归线。
答案 0 :(得分: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)
拟合的模型中获得的。
希望这就是你要找的东西。