在進(jìn)行回歸分析之前窗声,通常要畫散點(diǎn)圖看兩組的相關(guān)趨勢(shì)相恃,并添加擬合曲線,本文就描述一下笨觅,如何使用ggplot2來(lái)繪制散點(diǎn)圖拦耐,并添加擬合曲線及其95%CI,最后加上R方值和回歸方程见剩。
本文使用iris數(shù)據(jù)集進(jìn)行演示(多變量數(shù)據(jù)杀糯,包含不同的因子),首先我想通過(guò)構(gòu)建一個(gè)函數(shù)苍苞,可以生成一個(gè)數(shù)據(jù)框固翰,包括R平方,截距和系數(shù)(方便ggplot2作圖),這樣就可以將多個(gè)變量?jī)蓛芍g的回歸方程展示在一個(gè)圖中骂际。
構(gòu)建函數(shù):
#函數(shù)作用:輸入一個(gè)數(shù)據(jù)框疗琉,輸入兩列的列名以及因子列的列名,求不同因子(分組)之間的回歸系數(shù)
例如iris數(shù)據(jù)集:
這里我輸入兩列的列名(例如Sepal.Length和Sepal.Width)歉铝,再輸入因子列盈简,也就是最后一列,就可以分別得到三種不同的種屬中Sepal.Length和Sepal.Width的相關(guān)系數(shù)犯戏。
Add_R <- function(dataframe,x,y,factor){? ?#四個(gè)參數(shù)送火,數(shù)據(jù)框,第一列和第二列的列名先匪,因子列的列名
? cor <- data.frame()
? ? dataframe[,factor] <- as.factor(dataframe[,factor])
? ? lev <- levels(dataframe[,factor])
? ? for (i in c(1:length(lev))) {
? ? ? name <- lev[i]
? ? ? data <- dataframe[which(dataframe[,factor] == name),]
? ? ? lm <- summary(lm(data,formula = data[,y]~data[,x]))
? ? ? r_squared <- round(lm$r.squared,2)
? ? ? inter <- round(lm$coefficients[1,1],2)
? ? ? coefficients <- round(lm$coefficients[1,2],2)
? ? ? max_x <- max(data[,x])? ? ? #給出圖中添加文本的位置
? ? ? max_y <- max(data[,y])
? ? ? if(inter>0){
? ? ? ? eq <- substitute(""~R^2~"="~a~","~hat(y)~" = "~b%.%x+c~ "",list(a = r_squared,b = coefficients,c = inter))? ?#非常重要的一步种吸,把公式打包,在通過(guò)下面的as.charaster來(lái)把公式以字符串的形式表示呀非,因?yàn)間eom_text這個(gè)函數(shù)中的label參數(shù)只接受字符串類型的數(shù)據(jù)
? ? ? }
? ? ? else{
? ? ? ?inter <- abs(inter)
? ? ? ? eq <- substitute(""~R^2~"="~a~","~hat(y)~" = "~b%.%x-c~"",list(a = r_squared,b = coefficients,c = inter))
? ? ? }
? ? ? cor <- rbind(cor,cbind(rsqua = r_squared,coef = coefficients,intercept = inter,max_x = max_x,max_y = max_y,exp = ""))
? ? ? exp <- as.character(as.expression(eq))
? ? ? cor$exp[i] <- exp
? ? ? row.names(cor)[i] <- name
? ? }
? for (i in c(1:5)){
? ? cor[,i] <- as.numeric(cor[,i])? ? #轉(zhuǎn)換為數(shù)值向量
? }
? return(cor)
}
df <- Add_R(iris,"Sepal.Length","Sepal.Width","Species")? ?#求這兩列不同種屬的相關(guān)性
df
開始繪圖
library(ggplot2)? #加載包
ggplot(iris,aes(Sepal.Length,Sepal.Width))+? ?#和上面函數(shù)輸入的要對(duì)應(yīng)(df這個(gè)數(shù)據(jù)框)
? geom_point(size = 3,aes(color = Species,shape = Species,fill = Species))+
? geom_smooth(aes(color = Species,fill = Species),method = "lm",level = 0.95,formula = y~x,linetype = 2,alpha = 0.2)+
? scale_color_manual(values = c("slateblue2","blue4","tomato2"))+
? geom_text(data = df,aes(max_x,max_y,label = exp),vjust = 0,nudge_y = 0.1,size = 5,parse = T,color = c("slateblue2","blue4","tomato2"),)+? ? ? #注意parse = T參數(shù)坚俗,這個(gè)一定要等于T,才能把字符串類型的公式岸裙,以公式的形式表達(dá)猖败。
? coord_cartesian(xlim = c(4,9),expand = F,ylim = c(2,4.7))+
? theme(panel.background = element_blank(),
? ? ? ? panel.grid.major.y = element_line(colour = "grey",linetype = 2),
? ? ? ? axis.line = element_line(colour = "black",size = rel(2),arrow = arrow(angle = 30,length = unit(0.1,"inches"))),
? ? ? ? axis.title.y = element_text(size = rel(2),hjust = 0.5),
? ? ? ? axis.title.x = element_text(size = rel(2),hjust = 0.5),
? ? ? ? axis.text.x = element_text(size = rel(2),hjust = 1),
? ? ? ? axis.text.y = element_text(hjust = 1,size = rel(2)),
? ? ? ? axis.ticks = element_line(size = rel(1.3)),
? ? ? ? plot.title = element_text(size = rel(1.8)),
? ? ? ? plot.margin = margin(15,9,9,30))