r - How to get lines of best fit on multiple graphs? -


this question has answer here:

how seperate line of best fit on each of these graphs? feel free change code or use library- far got before got stuck. thanks!

exampledf<- data.frame(year=c("1999","1999","1999","1995","1995","1995"),npi=c(20,40,20,30,40,15),school=c("a","b","c","a","b","c")) library(lattice) library(car) with(exampledf,      xyplot(npi~year|school,xlab="year",ylab="npi",main="npi measurements school , year", aspect = "xy"),      abline(lm(npi~year|school,data=exampledf)) ) 

enter image description here

solution using xyplot:

add parameter type=c("p","r")

reference here adds both points , regression line each plot

how this, ggplot2? (note, added additional point each panel)

exampledf<- data.frame(year= rep(c("1999", "1995", "2005"), each = 3),    npi=c(20,40,20, 30,40,15, 15, 15, 30),    school=rep(c('a', "b", "c"), 3))  library(ggplot2)  ggplot(exampledf, aes(x = year, y = npi, group = school)) +      geom_point() +     geom_smooth(method = "lm") +     facet_wrap(~school) 

enter image description here


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -