IT408 / IT408 SC:
Data Mining

Unit 4: Data Visualization

R Batzinger

2026-06-08

Tidy models

Course Textbook:

Hadley Wickham,Mine Cetinkaya-Rundel and Garrett Grolemund, R for data science: import, tidy, transform, visualize, and model data. 2nd Edition, O’Reilly Press

Online version: https://r4ds.hadley.nz/ ## Schedule {.scrollable}

  • June
Su Mn Tu We Th Fr Sa
1 2 3 4 5 6
7 [8] 9 10 [11] 12 13
14 [15] 16 17 [18] 19 20
21 [22] 23 24 [25] 26 27
28 [29] 30
  • July
Su Mn Tu We Th Fr Sa
(1)* [[2]] 3 4
5 [6] 7 (8)* [9] 10 11
12 [13] 14 (15)* [16]L 17 18
21 [20] 21 22 [23] [[24]] 25
26 27 28 29 30 31
* IT408 Special Studies; L - Lab test

R Notebook

  • Title
  • Authors
  • Date
  • Abstract
  • Introduction
    • The nature of the problem
    • What work has been done before
    • Key Research Objectives
  • Methodology
  • Results
  • Discussion
  • Conclusion
    • Possible steps for future research
  • Bibliography

Scatterplot

plot(mtcars$wt, mtcars$mpg)

library(tidyverse)
library(gdata)
library(gcookbook)
qplot(wt, mpg, data=mtcars)

ggplot(mtcars, aes(x=wt, y=mpg,color=cyl)) + geom_point()

Line Graph

time = 1:200 
A= 20
p = 2
b =0.05

DrugA = A*time^p*exp(-b*time)
DrugB = A*time^p*exp(-sqrt(2)*b*time)
DrugC = 2*A*time^p*exp(-2*b*time)

  plot(time, DrugA, type="l",col="blue",lwd=2,
       main="Blood Concentration after Injection",
       xlab="Time (in minutes)",
       ylab="Units (per ml blood)")
  points(time,DrugA,col="blue")
  lines(time,DrugB, col="red", lwd=2)
  points(time,DrugB, col="red")
   lines(time,DrugC, col="orange", lwd=2)
   points(time,DrugC,col="orange")
   
   legend(140,4000,c("DrugA","DrugB","DrugC"),fill=c("blue","red","orange"))

drugdta = data.frame(time,DrugA,DrugB,DrugC)
drugdta |> ggplot(aes(x=time,y=DrugA)) + geom_line(aes(x=time,y=DrugA,col="DrugA")) + geom_point(aes(x=time,y=DrugA,color="DrugA")) + geom_line(aes(x=time,y=DrugB,color="DrugB")) + geom_point(aes(x=time,y=DrugB,color="DrugB")) +
  geom_line(aes(x=time,y=DrugC,color="DrugC")) + geom_point(aes(x=time,y=DrugC,color="DrugC")) + theme(legend.position=c(.9,.9),legend.justification=c(1,1),) +
  labs(x="Time (in minutes)", y="Drug Concentration",
       title="Comparison of Blood Concentrations after Injection")

Data Cleansing Exercise

df <- data.frame(
  Employee_ID = c(101, 102, 103, 104, 105, 106, 107, 103, 108, 109),
  Age = c(28, 34, 41, NA, 32, 29, 145, 41, 36, 31),
  Salary_USD = c(55000, 62000, 78000, 51000, 2, 58000,
      85000, 78000, 69000, 950000)
)


# ---------------------

df = df |> filter(Age < 120) |> distinct(Employee_ID,.keep_all =TRUE) |> filter(Salary_USD >20000)
df = df[1:5,]
df |> ggplot(aes(x=Age, y=Salary_USD)) + geom_point()

Graphic Types

Graphic Name Key use
WordCloud Word usage frequency
2D - Density
3 - D
Bar
Box
Bubble Map
Bubble Plot
Bundle
Chloropleth
Circular Barplot
Connected Map
Correlogram
Dendrogram
Density
<img src=“./IT408-4_files/Doughnut150.png Donut
Grouped Bar
Heatmap
Line
Pie
Sankey
Scatter Plot
Spider
Stacked Area
Tree
Violin

Calculating a BMI

\[BMI=\frac{10000wt}{ht^2}\] where

\[\matrix{BMI &=& \hbox{Body Mass Index}\hfil\cr wt &=& \hbox{weight in kg}\hfil\cr ht &=& \hbox{height in cm}\hfil\cr}\]

Another dirty dataset

bmi.dt   = read.csv("badtable.csv",header=TRUE, sep=",")
print(bmi.dt)
   ID                 Name Gender Age Height Weight   BMI
1   1          James Smith   Male  34  178.0     82  25.9
2   2             John Doe   Male  28  182.0     82  22.6
3   3       Robert Johnson   Male  45  175.0     90  29.4
4   4        Michael Brown   Male  22  190.0     85  23.5
5   5        William Jones   Male  31   17.0     68  23.5
6   6         David Miller   Male  50  165.0     72  26.4
7   7        Richard Davis      M  38  180.0     82  31.3
8   8        Joseph Garcia   Male  29  173.0     70  23.4
9   9     Thomas Rodriguez   Male  41  185.0     95  27.8
10 10       Charles Wilson   Male  63  171.0     78  26.7
11 11 Christopher Martinez   Male  26  177.0     80  25.5
12 12      Daniel Anderson   Male  35  181.0     88  26.9
13 13       Matthew Taylor      M  47  176.0     84  27.1
14 14       Anthony Thomas   Male  19  183.0     81  21.8
15 15           Mark Moore   Male  52  168.0     65  23.0
16 16       Donald Jackson   Male  44  174.0    360  52.8
17 17        Steven Martin   Male  33  179.0     82  24.7
18 18             Paul Lee   Male  58  172.0     81  27.4
19 19         Andrew Perez   Male  25  188.0     92  26.0
20 20      Joshua Thompson   Male  39  180.0     83  25.6
21 21        Kenneth White   Male  42  175.0     77  25.1
22 22         Kevin Harris   Male  30  182.0     86  36.0
23 23        Brian Sanchez   Male  36  178.0     79  23.4
24 24         George Clark   Male  48  169.0     71  24.9
25 25       Edward Ramirez   Male  55  184.0     89  23.6
26 26           Mary Smith Female  29  165.0     58  21.3
27 27       Patricia Jones      F  34  162.0     62  23.6
28 28       Jennifer Brown Female  41  170.0     70  24.2
29 29          Linda Davis Female  53  158.0     55  22.0
30 30     Elizabeth Miller Female  24  168.0     60  21.3
31 31       Barbara Wilson Female  37  163.0     74  32.0
32 32          Susan Moore Female  46  160.0     52  20.3
33 33       Jessica Taylor Female  31  172.0     67  22.6
34 34       Sarah Anderson Female  28  167.0     59  21.2
35 35         Karen Thomas Female  50  155.0     54  22.5
36 36        Nancy Jackson Female  62  161.0     63  24.3
37 37           Lisa White Female  23  174.0     68  22.5
38 38         Betty Harris Female  45  160.0     72  39.9
39 39      Margaret Martin Female  39  164.0     61  22.7
40 40        Sandra Garcia      F  33  171.0     64  21.9
41 41      Ashley Martinez Female  27  168.0     57  20.2
42 42    Kimberly Robinson Female  35  162.0     55    NA
43 43          Emily Clark Female  48  159.0     66  26.1
44 44      Donna Rodriguez Female  55  165.0     72  26.4
45 45       Michelle Lewis Female  22  170.0     58  20.1
46 46            Carol Lee Female  30  163.0     53  19.9
47 47        Amanda Walker Female  43  167.0     75  26.9
48 48       Stephanie Hall Female  26  172.0     69  23.3
49 49        Carolyn Allen female  32   16.5     60 220.3
50 50      Christine Young Female  51  160.0     62  34.2

Clean up {,scrollable}

library(stringr)
library(dplyr)
library(tidyverse)

bmi.dt = bmi.dt |> mutate(Gender = str_to_upper(Gender)) |>
 mutate(Gender = str_sub(Gender, 1, 1)) |>
  mutate(Height = if_else(Height < 100,Height * 10, Height))  |>
   mutate(BMI.Calc= round(10000*Weight/Height^2,1)) |>
   mutate(BMI2 = case_when(
     ID == 22 ~ 26.0,
     ID == 38 ~ 28.1,
     ID == 42 ~ 21.0, 
     ID == 49 ~ 22.0,
     ID == 50 ~ 24.2,
      TRUE     ~ BMI)) |>
  filter(BMI.Calc < 100) |>
 # select(ID, Gender, Age, Height, Weight, BMI) |>
  print()
   ID                 Name Gender Age Height Weight   BMI BMI.Calc BMI2
1   1          James Smith      M  34    178     82  25.9     25.9 25.9
2   2             John Doe      M  28    182     82  22.6     24.8 22.6
3   3       Robert Johnson      M  45    175     90  29.4     29.4 29.4
4   4        Michael Brown      M  22    190     85  23.5     23.5 23.5
5   5        William Jones      M  31    170     68  23.5     23.5 23.5
6   6         David Miller      M  50    165     72  26.4     26.4 26.4
7   7        Richard Davis      M  38    180     82  31.3     25.3 31.3
8   8        Joseph Garcia      M  29    173     70  23.4     23.4 23.4
9   9     Thomas Rodriguez      M  41    185     95  27.8     27.8 27.8
10 10       Charles Wilson      M  63    171     78  26.7     26.7 26.7
11 11 Christopher Martinez      M  26    177     80  25.5     25.5 25.5
12 12      Daniel Anderson      M  35    181     88  26.9     26.9 26.9
13 13       Matthew Taylor      M  47    176     84  27.1     27.1 27.1
14 14       Anthony Thomas      M  19    183     81  21.8     24.2 21.8
15 15           Mark Moore      M  52    168     65  23.0     23.0 23.0
16 17        Steven Martin      M  33    179     82  24.7     25.6 24.7
17 18             Paul Lee      M  58    172     81  27.4     27.4 27.4
18 19         Andrew Perez      M  25    188     92  26.0     26.0 26.0
19 20      Joshua Thompson      M  39    180     83  25.6     25.6 25.6
20 21        Kenneth White      M  42    175     77  25.1     25.1 25.1
21 22         Kevin Harris      M  30    182     86  36.0     26.0 26.0
22 23        Brian Sanchez      M  36    178     79  23.4     24.9 23.4
23 24         George Clark      M  48    169     71  24.9     24.9 24.9
24 25       Edward Ramirez      M  55    184     89  23.6     26.3 23.6
25 26           Mary Smith      F  29    165     58  21.3     21.3 21.3
26 27       Patricia Jones      F  34    162     62  23.6     23.6 23.6
27 28       Jennifer Brown      F  41    170     70  24.2     24.2 24.2
28 29          Linda Davis      F  53    158     55  22.0     22.0 22.0
29 30     Elizabeth Miller      F  24    168     60  21.3     21.3 21.3
30 31       Barbara Wilson      F  37    163     74  32.0     27.9 32.0
31 32          Susan Moore      F  46    160     52  20.3     20.3 20.3
32 33       Jessica Taylor      F  31    172     67  22.6     22.6 22.6
33 34       Sarah Anderson      F  28    167     59  21.2     21.2 21.2
34 35         Karen Thomas      F  50    155     54  22.5     22.5 22.5
35 36        Nancy Jackson      F  62    161     63  24.3     24.3 24.3
36 37           Lisa White      F  23    174     68  22.5     22.5 22.5
37 38         Betty Harris      F  45    160     72  39.9     28.1 28.1
38 39      Margaret Martin      F  39    164     61  22.7     22.7 22.7
39 40        Sandra Garcia      F  33    171     64  21.9     21.9 21.9
40 41      Ashley Martinez      F  27    168     57  20.2     20.2 20.2
41 42    Kimberly Robinson      F  35    162     55    NA     21.0 21.0
42 43          Emily Clark      F  48    159     66  26.1     26.1 26.1
43 44      Donna Rodriguez      F  55    165     72  26.4     26.4 26.4
44 45       Michelle Lewis      F  22    170     58  20.1     20.1 20.1
45 46            Carol Lee      F  30    163     53  19.9     19.9 19.9
46 47        Amanda Walker      F  43    167     75  26.9     26.9 26.9
47 48       Stephanie Hall      F  26    172     69  23.3     23.3 23.3
48 49        Carolyn Allen      F  32    165     60 220.3     22.0 22.0
49 50      Christine Young      F  51    160     62  34.2     24.2 24.2

Graphics