##借用語法
library(lattice)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
library(grid)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
library(hrbrthemes)
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
##使用資料庫WWGbook,搜尋資料autism
pacman::p_load(WWGbook)
data("autism", package="WWGbook")
dta <- WWGbook::autism
##查看資料類型
head(dta)
##   age vsae sicdegp childid
## 1   2    6       3       1
## 2   3    7       3       1
## 3   5   18       3       1
## 4   9   25       3       1
## 5  13   27       3       1
## 6   2   17       3       3
##新增新欄將sicdegp轉換成低中高
dta$sic<-factor(dta$sicdegp, levels = c(1,2,3), labels = c("L", "M", "H"))
##查看新增狀況
head(dta)
##   age vsae sicdegp childid sic
## 1   2    6       3       1   H
## 2   3    7       3       1   H
## 3   5   18       3       1   H
## 4   9   25       3       1   H
## 5  13   27       3       1   H
## 6   2   17       3       3   H
##先畫背景底圖,依照老師上課介紹將圖形一一加上去
p0 <- ggplot(data=dta, 
             aes(x=age, 
                 y=vsae))+
              geom_point()+
              geom_point(data=dta, 
                    aes(color=mean(dta)))+
      stat_smooth(data=dta, 
              aes(color=mean(dta)), 
              formula=y ~ x,
              method='lm', 
              se=FALSE)+
        labs(x='age_yr', 
             y='vsae_social', 
            title="Sequenced Inventory of Communication Development (SICD) ")+
            facet_grid(. ~ sicdegp)+
            theme_ipsum()+
            theme(legend.position='bottom')
p0
## Warning in mean.default(dta): argument is not numeric or logical: returning NA

## Warning in mean.default(dta): argument is not numeric or logical: returning NA

## Warning in mean.default(dta): argument is not numeric or logical: returning NA
## Warning: Removed 2 rows containing non-finite values (stat_smooth).
## Warning: Removed 2 rows containing missing values (geom_point).

## Warning: Removed 2 rows containing missing values (geom_point).
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

##na.omit刪除NA值,原因不了解
dta1<-na.omit(dta)  
##年齡-2原因不了解
p<-dta1 %>% mutate(age2=age-2)%>%group_by(sic, age2)%>%
  dplyr::summarise(n=n(), m_p=mean(vsae),se_p=sd(vsae)/sqrt(n))%>%
##運用上課老師介紹ggplot的邏輯和設定,將分組、畫圖bar、線、點,依序加進去
  ggplot() + 
  aes(age2, m_p, group=sic, shape=sic) +
  geom_errorbar(aes(ymin=m_p - se_p,
                    ymax=m_p + se_p),
                width=.2, size=.3,) +
  geom_line(aes(linetype=sic),show.legend=T)+
  geom_point(size=rel(3),show.legend=T) +
  scale_shape(guide=guide_legend(title=NULL)) +
  labs(x="age_yr", y="vsae_social") +
  theme_ipsum() +
  theme(legend.position=c(.9, .8))

p
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

output:
  prettydoc::html_pretty:
    theme: cayman
    highlight: github