-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheights_plot.R
More file actions
43 lines (35 loc) · 1.61 KB
/
heights_plot.R
File metadata and controls
43 lines (35 loc) · 1.61 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
students <- paste("Student", 1:5)
ages <- seq(6, 9, by = 0.5) # 6, 6.5, ..., 9
# Create a data frame of heights
data <- expand.grid(Age = ages, Student = students)
data$Height <- round(rnorm(nrow(data), mean = 120 + 5 * data$Age, sd = 2), 1)
# Base R plot
plot(data$Age, data$Height, type = "n",
xlab = "Age (years)", ylab = "Height (cm)",
main = "Height Measurements")
colors <- rainbow(length(students))
# Add points and lines per student
for (i in seq_along(students)) {
subset_data <- subset(data, Student == students[i])
lines(subset_data$Age, subset_data$Height, col = colors[i], lwd = 2)
points(subset_data$Age, subset_data$Height, col = colors[i], pch = 16)
}
legend("topleft", legend = students, col = colors, pch = 16, lty = 1, lwd = 2)
set.seed(456) # For reproducibility
students <- paste("Student", 1:5)
ages <- seq(6, 9, by = 0.5) # 6, 6.5, ..., 9
# Create a data frame of weights
data_w <- expand.grid(Age = ages, Student = students)
data_w$Weight <- round(rnorm(nrow(data_w), mean = 20 + 2.5 * data_w$Age, sd = 1.5), 1)
# Base R plot
plot(data_w$Age, data_w$Weight, type = "n",
xlab = "Age (years)", ylab = "Weight (kg)",
main = "Longitudinal Weight Measurements")
colors <- rainbow(length(students))
# Add points and lines per student
for (i in seq_along(students)) {
subset_data <- subset(data_w, Student == students[i])
lines(subset_data$Age, subset_data$Weight, col = colors[i], lwd = 2)
points(subset_data$Age, subset_data$Weight, col = colors[i], pch = 16)
}
legend("topleft", legend = students, col = colors, pch = 16, lty = 1, lwd = 2)