data<-data.frame(x,y)reg<-lm(formula=y~x,data=data)#get intercept and slope valuecoeff<-coefficients(reg)intercept<-coeff[1]slope<-coeff[2]# Create basic ggplotggp<-ggplot(data,aes(x,y))+geom_point()# add the regression lineggp+geom_abline(intercept=intercept,slope=slope,color="red"...
# Load the ggplot2 package library(ggplot2) # Create a scatterplot ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() # Add a regression line ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_smooth(method = "lm", color = "red", linetype = "dashed", size...
#Add regression line conf.int = TRUE, #Add confidence interval color = "cyl", palette = "jco",#Color by group cyl shape = "cyl" #Change point shape by groups cyl )+ stat_cor(aes(color=cyl), label.x = 3) #Add correlation coefficientsp ...
https://stackoverflow.com/questions/7549694/add-regression-line-equation-and-r2-on-graph 首先是模拟一份数据集 代码语言:javascript 复制 df<-data.frame(x = c(1:100)) df$y <- 2 + 3 * df$x + rnorm(100, sd = 40) head(df) ggplot2基本的散点图并添加拟合曲线 代码语言:javascript 复制...
Add regression line, correlation coefficient and equantions of the fitted line. Key functions: stat_smooth()[ggplot2] stat_cor()[ggpubr] stat_poly_eq()[ggpmisc] formula <- y ~ xp + stat_smooth( aes(color = Species, fill = Species), method = "lm") + stat_cor(aes(color = Species...
#Scatterplots(sp)sp<-ggscatter(mtcars,x="wt",y="mpg",add="reg.line",#Add regression line conf.int=TRUE,#Add confidence interval color="cyl",palette="jco",#Color by group cyl shape="cyl"#Change point shape by groups cyl)+stat_cor(aes(color=cyl),label.x=3)#Add correlation coeffici...
# loess method: local regression fitting p3 <- ggscatter(df, x = "wt", y = "mpg", add = "loess", conf.int = TRUE, cor.coef = TRUE, # Add correlation coefficient. see ?stat_cor cor.coeff.args = list(method = "spearma...
add = "reg.line", # Add regression line conf.int = TRUE, # Add confidence interval color = "cyl", palette = "jco", # Color by groups "cyl" shape = "cyl" # Change point shape by groups "cyl" )+ stat_cor(aes(color = cyl), label.x = 3) # Add correlation coefficient sp #...
ggparsort.val="asc",# Sort the value in ascending ordersort.by.groups=TRUE,# Sort inside each groupx.text.angle=90# Rotate vertically x axis texts)bp+font("x.text",size=8)# Scatter plots (sp)sp<-ggscatter(mtcars,x="wt",y="mpg",add="reg.line",# Add regression lineconf.int=...
#Scatterplots(sp)sp<-ggscatter(mtcars,x="wt",y="mpg",add="reg.line",#Add regression line conf.int=TRUE,#Add confidenceintervalcolor="cyl",palette="jco",#Colorbygroupcylshape="cyl"#Change point shape by groups cyl)+stat_cor(aes(color=cyl),label.x=3)#Add correlation coefficientsp ...