以对数刻度(半)R绘制数据的置信区间

时间:2018-09-27 16:08:34

标签: r ggplot2 regression linear-regression

我有一个带有x,y列和类型(A和B)的数据框。我想以对数刻度绘制x轴,然后以每种类型的拟合置信区间绘制数据的线性拟合。

df = structure(list(x = c(21.9247706844111, 22.5845455758455, 23.2842999484095, 24.0277818926186, 24.819223123522, 25.6634195847913, 26.565828729518, 27.5326876468961, 28.5711574392776, 29.6895009170119, 30.8973029400445, 32.2057458449174, 33.6279567222896, 35.1794494065101, 36.8786927479251, 38.7478493668773, 40.8137476984428, 43.1091780520793, 45.6746461040917, 48.5607839497721, 51.8317255763963, 55.569928967504, 59.88322379377, 64.9153829493109, 70.8624603805235, 77.9989316122244, 86.7212612912185, 97.6241463875416, 111.642110711055, 130.332693944635, 156.499467517187, 195.74957428619, 261.166347585308, 391.999787414874, 784.499893775217 ), y = c(14609.8039776394, 13641.5276286484, 14137.4346361605, 14831.2202848298, 14230.1838512313, 14145.1057803085, 14902.02786392, 15437.3601780532, 14770.6352337797, 14943.8860826451, 14556.4478379308, 15436.3188023377, 15921.0025288811, 15515.4269474865, 15413.4117571918, 15718.3707317223, 15867.2190057381, 15979.0338298445, 16508.9065832565, 17054.8192844372, 15725.1022904028, 16227.8629957584, 17457.9442545086, 17103.5685525182, 16768.2324312475, 17704.5819011923, 17067.0942588413, 17970.3457933202, 18373.8502710841, 18025.3306817536, 19324.7785137896, 19218.4350330616, 19543.9264336923, 22678.8476569314, 25119.8112666139 ), type = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "A", "A", "A", "A", "B", "A", "B", "A", "A", "A", "A", "A", "B", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A")), .Names = c("x", "y", "type"), row.names = 750:784, class = "data.frame")

到目前为止,我设法做到了(尽管我不确定它是否正确)

plot(df[,c(1:2)],log='x')
fit=lm(data=df, y~ log10(x))
newx <- seq(min(df$x), max(df$x), length.out=100)
preds <- predict(fit, newdata = data.frame(x=newx), 
                 ,level=0.95,interval = 'confidence')
polygon(c(rev(newx), newx), c(rev(preds[ ,3]), preds[ ,2]), col = rgb(0, 0, 0,0.1), border = NA)
lines(newx, preds[ ,3], lty = 'dashed', col = 'red')
lines(newx, preds[ ,2], lty = 'dashed', col = 'red')
abline(fit,col="black",lwd=2)

如何在同一张图中分别绘制类型(A和B)的CI。

有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

这是我对ggplot的处理方式,尽管我不确定我是否喜欢这种结果。

ggplot(df, aes(x, y, color = type)) +
 geom_point() +
 geom_smooth(method = "lm") +
 scale_x_log10(breaks = c(20, 50, 100, 200, 500))

enter image description here

我认为最好将它们并排分成两个图形,如下所示:

ggplot(df, aes(x, y, color = type)) +
 geom_point() +
 geom_smooth(method = "lm") +
 scale_x_log10(breaks = c(20, 50, 100, 200, 500)) +
 facet_wrap(~ type)

enter image description here

答案 1 :(得分:1)

尝试一下:

library(lme4)
fits=lmList(data=df, y~ log10(x)|type)
newxA <- seq(min(df[which(df$type=="A"),]$x), max(df[which(df$type=="A"),]$x), length.out=100)
newxB <- seq(min(df[which(df$type=="B"),]$x), max(df[which(df$type=="B"),]$x), length.out=100)
predsA <- predict(fits$A, newdata = data.frame(x=newxA), 
             ,level=0.95,interval = 'confidence')
predsB <- predict(fits$B, newdata = data.frame(x=newxB), 
              ,level=0.95,interval = 'confidence')
plot(df[which(df$type=="A"),c(1:2)],log='x')
polygon(c(rev(newxA), newxA), c(rev(predsA[ ,3]), predsA[ ,2]), col = rgb(0, 0, 0,0.1), border = NA)
lines(newxA, predsA[ ,3], lty = 'dashed', col = 'red')
lines(newxA, predsA[ ,2], lty = 'dashed', col = 'red')
abline(fits$A,col="black",lwd=2)
points(df[which(df$type=="B"),c(1:2)],log='x',add=TRUE)
polygon(c(rev(newxB), newxB), c(rev(predsB[ ,3]), predsB[ ,2]), col = rgb(0, 0, 0,0.1), border = NA)
lines(newxB, predsB[ ,3], lty = 'dashed', col = 'blue')
lines(newxB, predsB[ ,2], lty = 'dashed', col = 'blue')
abline(fits$B,col="purple",lwd=2)