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()
}
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)
footlength_cm actual_height_in point_label
1 24.0 56 A
2 26.0 69 B
3 30.0 67 C
4 29.0 77 D
5 35.0 73 E
6 22.0 63 <NA>
7 24.0 66 <NA>
8 26.0 62 <NA>
9 26.0 65 <NA>
10 26.5 67 <NA>
11 27.0 64 <NA>
12 28.0 64 <NA>
13 28.0 66 <NA>
14 29.0 65 <NA>
15 29.5 70 <NA>
16 30.0 74 <NA>
17 31.0 70 <NA>
18 32.0 74 <NA>
19 33.0 71 <NA>
20 34.0 72 <NA>
Call:
lm(formula = as.formula(formula_string), data = df)
Coefficients:
(Intercept) footlength_cm
38.30 1.03
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.
This chart has title 'HW 10.3 #3 foot length vs height'.
It has x-axis 'foot length (cm)' with labels 24, 28 and 32.
It has y-axis 'height (in)' with labels 55, 60, 65, 70 and 75.
The chart is a set of 20 big solid circle points of which about 100% can be seen.
It has size set to 4.
This chart has title 'HW 10.3 #3 foot length vs height'.
It has x-axis 'foot length (cm)' with labels 24, 28 and 32.
It has y-axis 'height (in)' with labels 55, 60, 65, 70 and 75.
It has 2 layers.
Layer 1 is a set of 20 big solid circle points of which about 100% can be seen.
Layer 1 has size set to 4.
Layer 2 is a 'lm' smoothed curve.Layer 2 has colour set to black.
Layer 2 has size set to 2.
This chart has title 'HW 10.3 #3 foot length vs height'.
It has x-axis 'foot length (cm)' with labels 24, 28 and 32.
It has y-axis 'height (in)' with labels 55, 60, 65, 70 and 75.
It has 2 layers.
Layer 1 is a set of 20 big solid circle points of which about 100% can be seen.
Layer 1 has size set to 4.
Layer 2 is a 'lm' smoothed curve.Layer 2 has colour set to black.
Layer 2 has size set to 2.
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.
png
2
model <- lm( actual_height_in ~ footlength_cm, data = biometrics )
print(model)
Call:
lm(formula = actual_height_in ~ footlength_cm, data = biometrics)
Coefficients:
(Intercept) footlength_cm
38.30 1.03
# 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()
png
2