Download the data file from the language and math example first to a data folder. In Rstudio, open the R script and compile a report in html directly or use the command > knitr::spin(“foo.R”, knit=FALSE)
to render it as an RMD file format first.
# data management and graphics package
library(tidyverse)
# input data
dta <- read.csv("C:/Users/Ching-Fang Wu/Documents/lmm/langMath.csv", h=T)
# compute averages by school
dta_a <- dta %>%
group_by(School) %>%
summarize(ave_lang = mean(Lang, na.rm=TRUE),
ave_arith = mean(Arith, na.rm=TRUE))
# superimpose two plots
ggplot(data=dta, aes(x=Arith, y=Lang)) +
geom_point(color="skyblue") +
stat_smooth(method="lm", formula=y ~ x, se=F, col="skyblue") +
geom_point(data=dta_a, aes(ave_arith, ave_lang), color="steelblue") +
stat_smooth(data=dta_a, aes(ave_arith, ave_lang),
method="lm", formula= y ~ x, se=F, color="steelblue") +
labs(x="Arithmetic score",
y="Language score") +
theme_bw()