Chapter 23 Challenge 4

Generate a plot of comparing the CO2 concentration versus uptake where each plant is shown using a different point.

Bonus points for doing this challenge using nested loops!

Challenge 4: Answer

plot(x = CO2$conc, y = CO2$uptake, type = "n", cex.lab = 1.4,
    xlab = "CO2 concentration", ylab = "CO2 uptake")

# Type 'n' tells R to not actually plot the points.

plants <- unique(CO2$Plant)

for (i in 1:nrow(CO2)) {
    for (p in 1:length(plants)) {
        if (CO2$Plant[i] == plants[p]) {
            points(CO2$conc[i], CO2$uptake[i], col = p)
        }
    }
}
plot(x = CO2$conc, y = CO2$uptake, type = "n", cex.lab = 1.4,
    xlab = "CO2 Concentration", ylab = "CO2 Uptake")

plants <- unique(CO2$Plant)

for (i in 1:nrow(CO2)) {
    for (p in 1:length(plants)) {
        if (CO2$Plant[i] == plants[p]) {
            points(CO2$conc[i], CO2$uptake[i], col = p)
        }
    }
}