Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

ggplot2: how to get values for the regression line equation, r^2 and p value?

I cant work out how to get the regression line equation, r^2 and p value of the linear regression I have plotted using the function geom_smooth.

This is my code:

   g <- ggplot(data=data.male, aes(x=mid_year, y=mean_tc, colour=data.male$survey_type))  
   g <- g + geom_point(shape = 20, size =2) 
   g <- g + geom_smooth(method=lm, na.rm = FALSE, se = TRUE, aes(group=1), colour = "black")
   g <- g + theme_gray(base_size=24)
   g <- g+ xlab("Year")
   g <- g + ylab("Mean serum total cholesterol (mmol/L)")
   g <- g + theme(legend.position="bottom")
   g <- g + scale_y_continuous(limits=c(3.5,6.5), breaks=c(3.5,4,4.5,5,5.5,6,6.5))
   g <- g + scale_x_continuous(limits=c(1980,2015), breaks=c(1980,1990,2000,2010))
   g <- g + scale_colour_manual(name = "Survey Type", values= c("Red", "Blue", "Green")) 
   g  

[1]:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't use a plotting function for modelling. Fit the model using the lm function.

Then use the summary method to get everything you need to know about the fit.

You should get the same results as the plotting function, which I suspect uses lm internally.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...