---
title: "Homework Chapter 10.3"
author: "Dr. Paul Pearson"
date: "2024-11-01"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, comment = "", warning = FALSE, message = FALSE)

library(ggplot2) 
# ggsave(ggplot_object) can save svg files without xml annotations

library(BrailleR) 
# VI(ggplot_object) for annotations for in text graph annotations
# MakeAccessibleSVG(ggplot_object) to save accessible standalone html (=svg + xml annotations)

library(tactileR) 
# Use braille in graphs for embossed printing
# brl_begin(file='tactile_histogram_SWL.pdf', pt=14, paper='special', font='SWL')
# base or lattice plot code here
# brl_end()

library(infer)
#library(tibble)

library(tidyr) # for reshaping data, etc.

library(car) # for fancy scatterplots

set.seed(1)
options(digits = 4)
```



# Define helper functions

```{r}
make_graphs <-  function(df, explanatory, response, title_string, xlab_string, ylab_string, filename) {

  formula_string <- paste(substitute(response), "~", substitute(explanatory))
  # winner: https://win-vector.com/2019/07/06/programming-over-lm-in-r/
  # https://stackoverflow.com/questions/27261232/passing-argument-to-lm-in-r-within-function
  model <- eval(bquote(   lm( as.formula(formula_string), data = df)   ))
  print(model)  
  
  # ggplot scatterplot without line
  sp <- ggplot(df, aes( x = {{ explanatory }}, y = {{ response }} )) +
    geom_point(size = 4) + 
    ggtitle( title_string ) +
    xlab( xlab_string ) + 
    ylab( ylab_string ) +
    theme_bw(base_size = 18)
  
  print(sp) # generates both graph and VI(ts) automatically 
  
  # ggplot scatterplot with line
  sp <- sp + geom_smooth(method='lm', se=FALSE, size=2, color = "black")

  print(sp) # generates both graph and VI(ts) automatically
  
  MakeAccessibleSVG.ggplot(sp, file = paste0("reader-", filename))

  # tactile scatterplot with no line  
  brl_begin(paste0( 'tactile-', filename, '-scatterplot.pdf'), 
          pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
    # to get the variable values, use data[[substitute(varname)]] instead of data$varname
    plot(df[[substitute(explanatory)]],
         df[[substitute(response)]],
         main = title_string,
         xlab = xlab_string,
         ylab = ylab_string
         )
  brl_end()
  

  # tactile scatterplot with line  
  brl_begin(paste0( 'tactile-', filename, '-scatterplot-and-line.pdf'), 
          pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
    # to get the variable values, use data[[substitute(varname)]] instead of data$varname
    plot(df[[substitute(explanatory)]],
         df[[substitute(response)]],
         main = title_string,
         xlab = xlab_string,
         ylab = ylab_string
         )
    abline( model, col = "black", lwd = 3 )
  brl_end()
  

  # tactile scatterplot with line and ellipses and boxplots 
  brl_begin(paste0( 'tactile-', filename, '-scatterplot-and-line-and-ellipses-and-boxplots.pdf'), 
         pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
  scatterplot( as.formula(formula_string), data = df,
      main = title_string,
      xlab = xlab_string,
      ylab = ylab_string,
             pch = 16, cex = 2, col = "black",
             smooth = FALSE, grid = FALSE, frame = TRUE,
             ellipse = list(levels=c(.25, .75), robust=TRUE, fill=FALSE, fill.alpha=0, lwd = 3) )
  brl_end()


}

```



# 10.3.3 footlength vs height

```{r}
biometrics <- read.table(text=
"footlength_cm	actual_height_in	point_label
24	56	A
26	69	B
30	67	C
29	77	D
35	73	E
22	63 NA	
24	66 NA	
26	62 NA	
26	65 NA	
26.5	67 NA	
27	64 NA	
28	64 NA	
28	66 NA	
29	65 NA	
29.5	70 NA	
30	74 NA	
31	70 NA	
32	74 NA	
33	71 NA	
34	72 NA", header=TRUE, check.names=FALSE)

print(biometrics, row.names=TRUE)
```


```{r, echo=FALSE, fig.cap="This scatter plot has title ‘foot length versus height’ with x-axis ‘foot length (cm)’ that has values from 24 to 36 centimeters, and y-axis ‘height (in)’ with values from 55 to 80 inches.  There are 20 big solid circle points. The points are somewhat spread out but do show a clear increase in height as foot length increases."}
make_graphs( df = biometrics, 
             explanatory = footlength_cm, 
             response = actual_height_in, 
             title = "HW 10.3 #3 foot length vs height", 
             xlab_string = "foot length (cm)", 
             ylab_string = "height (in)",
             filename = "HW_10.3.3"
             )
```


```{r}
model <- lm( actual_height_in ~ footlength_cm, data = biometrics )
print(model)  

# tactile scatterplot with line  
brl_begin( 'tactile-HW_10.3.3-scatterplot-and-labels-and-line.pdf', 
     pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
  plot(biometrics$footlength_cm,
       biometrics$actual_height_in,
       type = "n", # remove all the points
       main = "HW 10.3.3 foot length vs height",
       xlab = "foot length (cm)",
       ylab = "height (in)"
       )
  abline( model, col = "black", lwd = 3 )
  points(biometrics$footlength_cm[1:5], biometrics$actual_height_in[1:5], 
         cex = 1.5, # character expansion factor
         pch = 17, # 3 = +, 17 = filled triangle
         )
  text(biometrics$footlength_cm[1:5], biometrics$actual_height_in[1:5], labels=biometrics$point_label[1:5], 
       cex = 1.25, # 1 = character expansion factor
       pos = 2 #  2 = left of point
       )
brl_end()
```






