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"...
2. 添加回归线 # Fit regression linerequire(stats)reg<-lm(mpg~wt,data=mtcars)reg coeff<-coefficients(reg)# Equation of the line :eq<-paste0("y = ",round(coeff[2],1),"*x + ",round(coeff[1],1))# Plotsp+geom_abline(intercept=37,slope=-5)+ggtitle(eq)# Change line type, color...
利用geom_smooth()添加回归线 # 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",...
# Scatter Plotlibrary(ggplot2)ggplt<-ggplot(Orange,aes(x=circumference,y=age))+geom_point()+theme_classic()ggplt# Plotting a single Regression Lineggplt+geom_smooth(method=lm,se=FALSE,fullrange=TRUE) R Copy 输出 这是一个单一的平滑线,或俗称为回归线。在这里,各点是结合在一起的,没有在任...
Scatter plot with linear regression line of best fit 图1,显示不同类别 df <- ggplot2::mpg %>% setDT() df_select <- df[cyl %in% c(4,8),] %>% .[,cyl:=as.factor(cyl)] cyl_color <- c("#1f77b4", "#ff983e") # geom_smooth的填充范围,只有数据和全图可选,而且se只会按垂直方向...
Line types Themes and background colors Axis scales and transformations Axis ticks: customize tick marks and labels, reorder and select items Add straight lines to a plot: horizontal, vertical and regression lines Rotate a plot: flip and reverse ...
#Scatter plots(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), ...
#Scatter plots(sp)sp <- ggscatter(mtcars, x="wt", y="mpg",add="reg.line",#Add regressionlineconf.int= TRUE,#Add confidence intervalcolor ="cyl", palette ="jco",#Color by group cylshape ="cyl"#Change point shape by groups cyl)+ ...
#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 ...
(mtcars, x = "wt", y = "mpg", 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 = ...