##load and see
dta <- Ecdat::Caschool

head(dta)
##   distcod  county                        district grspan enrltot teachers
## 1   75119 Alameda              Sunol Glen Unified  KK-08     195    10.90
## 2   61499   Butte            Manzanita Elementary  KK-08     240    11.15
## 3   61549   Butte     Thermalito Union Elementary  KK-08    1550    82.90
## 4   61457   Butte Golden Feather Union Elementary  KK-08     243    14.00
## 5   61523   Butte        Palermo Union Elementary  KK-08    1335    71.50
## 6   62042  Fresno         Burrel Union Elementary  KK-08     137     6.40
##   calwpct mealpct computer testscr   compstu  expnstu      str    avginc
## 1  0.5102  2.0408       67  690.80 0.3435898 6384.911 17.88991 22.690001
## 2 15.4167 47.9167      101  661.20 0.4208333 5099.381 21.52466  9.824000
## 3 55.0323 76.3226      169  643.60 0.1090323 5501.955 18.69723  8.978000
## 4 36.4754 77.0492       85  647.70 0.3497942 7101.831 17.35714  8.978000
## 5 33.1086 78.4270      171  640.85 0.1280899 5235.988 18.67133  9.080333
## 6 12.3188 86.9565       25  605.55 0.1824818 5580.147 21.40625 10.415000
##       elpct readscr mathscr
## 1  0.000000   691.6   690.0
## 2  4.583333   660.5   661.9
## 3 30.000002   636.3   650.9
## 4  0.000000   651.9   643.5
## 5 13.857677   641.8   639.9
## 6 12.408759   605.7   605.4
str(dta)
## 'data.frame':    420 obs. of  17 variables:
##  $ distcod : int  75119 61499 61549 61457 61523 62042 68536 63834 62331 67306 ...
##  $ county  : Factor w/ 45 levels "Alameda","Butte",..: 1 2 2 2 2 6 29 11 6 25 ...
##  $ district: Factor w/ 409 levels "Ackerman Elementary",..: 362 214 367 132 270 53 152 383 263 94 ...
##  $ grspan  : Factor w/ 2 levels "KK-06","KK-08": 2 2 2 2 2 2 2 2 2 1 ...
##  $ enrltot : int  195 240 1550 243 1335 137 195 888 379 2247 ...
##  $ teachers: num  10.9 11.1 82.9 14 71.5 ...
##  $ calwpct : num  0.51 15.42 55.03 36.48 33.11 ...
##  $ mealpct : num  2.04 47.92 76.32 77.05 78.43 ...
##  $ computer: int  67 101 169 85 171 25 28 66 35 0 ...
##  $ testscr : num  691 661 644 648 641 ...
##  $ compstu : num  0.344 0.421 0.109 0.35 0.128 ...
##  $ expnstu : num  6385 5099 5502 7102 5236 ...
##  $ str     : num  17.9 21.5 18.7 17.4 18.7 ...
##  $ avginc  : num  22.69 9.82 8.98 8.98 9.08 ...
##  $ elpct   : num  0 4.58 30 0 13.86 ...
##  $ readscr : num  692 660 636 652 642 ...
##  $ mathscr : num  690 662 651 644 640 ...
help(Caschool)
## starting httpd help server ... done
#create ratio 

ratio <- dta$enrltot/dta$teachers
head(round(ratio,2))
## [1] 17.89 21.52 18.70 17.36 18.67 21.41
cbindratio <-round(ratio,2)
cdta <- cbind(dta,cbindratio)
head(cdta,10)
##    distcod      county                        district grspan enrltot teachers
## 1    75119     Alameda              Sunol Glen Unified  KK-08     195    10.90
## 2    61499       Butte            Manzanita Elementary  KK-08     240    11.15
## 3    61549       Butte     Thermalito Union Elementary  KK-08    1550    82.90
## 4    61457       Butte Golden Feather Union Elementary  KK-08     243    14.00
## 5    61523       Butte        Palermo Union Elementary  KK-08    1335    71.50
## 6    62042      Fresno         Burrel Union Elementary  KK-08     137     6.40
## 7    68536 San Joaquin           Holt Union Elementary  KK-08     195    10.00
## 8    63834        Kern             Vineland Elementary  KK-08     888    42.50
## 9    62331      Fresno        Orange Center Elementary  KK-08     379    19.00
## 10   67306  Sacramento     Del Paso Heights Elementary  KK-06    2247   108.00
##    calwpct  mealpct computer testscr    compstu  expnstu      str    avginc
## 1   0.5102   2.0408       67  690.80 0.34358975 6384.911 17.88991 22.690001
## 2  15.4167  47.9167      101  661.20 0.42083332 5099.381 21.52466  9.824000
## 3  55.0323  76.3226      169  643.60 0.10903226 5501.955 18.69723  8.978000
## 4  36.4754  77.0492       85  647.70 0.34979424 7101.831 17.35714  8.978000
## 5  33.1086  78.4270      171  640.85 0.12808989 5235.988 18.67133  9.080333
## 6  12.3188  86.9565       25  605.55 0.18248175 5580.147 21.40625 10.415000
## 7  12.9032  94.6237       28  606.75 0.14358975 5253.331 19.50000  6.577000
## 8  18.8063 100.0000       66  609.00 0.07432432 4565.746 20.89412  8.174000
## 9  32.1900  93.1398       35  612.50 0.09234829 5355.548 19.94737  7.385000
## 10 78.9942  87.3164        0  612.65 0.00000000 5036.211 20.80556 11.613333
##        elpct readscr mathscr cbindratio
## 1   0.000000   691.6   690.0      17.89
## 2   4.583333   660.5   661.9      21.52
## 3  30.000002   636.3   650.9      18.70
## 4   0.000000   651.9   643.5      17.36
## 5  13.857677   641.8   639.9      18.67
## 6  12.408759   605.7   605.4      21.41
## 7  68.717949   604.5   609.0      19.50
## 8  46.959461   605.5   612.5      20.89
## 9  30.079157   608.9   616.1      19.95
## 10 40.275921   611.9   613.4      20.81
#grouping
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(lattice)

quantile(dta$readscr,seq(0, 1, 1/3))
##        0% 33.33333% 66.66667%      100% 
##  604.5000  645.3667  664.0667  704.0000
levels <- with(cdta, cut(readscr, ordered=T, breaks=c(0, 645, 664, 710), labels=c("L", "M","H")))
newdta <- cbind(cdta,levels)

newdta2 <- filter(newdta,grspan %in% c("KK-08"))
#xyplot
xyplot(readscr ~ cbindratio | levels, 
       data=newdta2, 
       xlab="Student-Teacher ratio", 
       ylab="Reading score",
       type=c('p', 'g', 'r'),
       layout=c(3, 1),pch=1)

#