Load and Prepare Data
setwd("~/Downloads/Intro to R/Module 8")
theoph <- read.csv("theoph.csv")
theoph$Subject <- factor(theoph$Subject)
Select Relevant Variables
df_subject_time_conc <- theoph |>
select(Subject, Time, conc)
df_subject_info <- theoph |>
select(Subject, Wt, Dose) |>
distinct()
Filter for Specific Subjects
first_subject <- theoph |>
filter(Subject == levels(theoph$Subject)[1])
first_four_subjects <- theoph |>
filter(Subject %in% levels(theoph$Subject)[1:4])
Summary Statistics
avg_conc_per_subject <- theoph |>
group_by(Subject) |>
summarise(avg_conc = mean(conc, na.rm = TRUE))
avg_conc_per_subject
## # A tibble: 12 × 2
## Subject avg_conc
## <fct> <dbl>
## 1 1 6.44
## 2 2 4.82
## 3 3 5.09
## 4 4 4.94
## 5 5 5.78
## 6 6 3.53
## 7 7 3.91
## 8 8 4.27
## 9 9 4.89
## 10 10 5.93
## 11 11 4.51
## 12 12 5.41
Line Plot – First Subject
ggplot(first_subject, aes(x = Time, y = conc)) +
geom_line() +
ggtitle("Concentration Over Time – First Subject") +
theme_minimal()

Line Plot – All Subjects Colored
ggplot(df_subject_time_conc, aes(x = Time, y = conc, color = Subject)) +
geom_line() +
ggtitle("Concentration Over Time by Subject") +
theme_minimal()

Faceted Plot by Subject
ggplot(df_subject_time_conc, aes(x = Time, y = conc)) +
geom_line() +
facet_wrap(~ Subject) +
ggtitle("Faceted Plot: Concentration Over Time by Subject") +
theme_minimal()
