R Markdown
library(aqm)
##
## Attaching package: 'aqm'
## The following object is masked from 'package:stats':
##
## dt
library(ggplot2)
data(sleep)
sleep$logBodyWt <- log10(sleep$BodyWt)
sleep$logBrainWt <- log10(sleep$BrainWt)
dt(sleep)
fig1<-ggplot(sleep,aes(x=logBodyWt,y=logBrainWt)) +geom_point()
fig1
fig1 +geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
fig1 +geom_smooth(method="lm")
mod<-lm(data=sleep,logBrainWt~logBodyWt)
summary(mod)
##
## Call:
## lm(formula = logBrainWt ~ logBodyWt, data = sleep)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.74953 -0.22133 -0.02063 0.18531 0.82617
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.92489 0.04670 19.80 <2e-16 ***
## logBodyWt 0.76407 0.03153 24.23 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3168 on 51 degrees of freedom
## Multiple R-squared: 0.9201, Adjusted R-squared: 0.9185
## F-statistic: 587.2 on 1 and 51 DF, p-value: < 2.2e-16
sleep$residuals<-residuals(mod)
sleep$rank<-rank(-sleep$residuals)
dt(sleep)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
fig1 <- ggplot(sleep,aes(x=logBodyWt,y=logBrainWt, label=Species)) +geom_point()
fig1 + geom_smooth(method="lm") ->fig2
ggplotly(fig2)