Forums | OpenR

OpenR: R & Statistics /
ANCOVA: shared code for graph of fruit production against root size


Peng Zhao's profile picture
Posts: 128

10 March 2022, 3:39 PM

library(ggplot2)
library(dplyr)
# df1 <- read.table("ipomopsis.txt",header=T)
df1 <- structure(list(Root = c(6.225, 6.487, 4.919, 5.13, 5.417, 5.359,
7.614, 6.352, 4.975, 6.93, 6.248, 5.451, 6.013, 5.928, 6.264,
7.181, 7.001, 4.426, 7.302, 5.836, 10.253, 6.958, 8.001, 9.039,
8.91, 6.106, 7.691, 8.988, 8.975, 9.844, 8.508, 7.354, 8.643,
7.916, 9.351, 7.066, 8.158, 7.382, 8.515, 8.53),
Fruit = c(59.77,
60.98, 14.73, 19.28, 34.25, 35.53, 87.73, 63.21, 24.25, 64.34,
52.92, 32.35, 53.61, 54.86, 64.81, 73.24, 80.64, 18.89, 75.49,
46.73, 116.05, 38.94, 60.77, 84.37, 70.11, 14.95, 70.7, 80.31,
82.35, 105.07, 73.79, 50.08, 78.28, 41.48, 98.47, 40.15, 52.26,
46.64, 71.01, 83.03),
Grazing = c("Ungrazed", "Ungrazed", "Ungrazed",
"Ungrazed", "Ungrazed", "Ungrazed", "Ungrazed", "Ungrazed", "Ungrazed",
"Ungrazed", "Ungrazed", "Ungrazed", "Ungrazed", "Ungrazed", "Ungrazed",
"Ungrazed", "Ungrazed", "Ungrazed", "Ungrazed", "Ungrazed", "Grazed",
"Grazed", "Grazed", "Grazed", "Grazed", "Grazed", "Grazed", "Grazed",
"Grazed", "Grazed", "Grazed", "Grazed", "Grazed", "Grazed", "Grazed",
"Grazed", "Grazed", "Grazed", "Grazed", "Grazed")),
class = "data.frame",
row.names = c(NA,-40L))

for (grazing in unique(df1$Grazing)){
print(lm(Fruit ~ Root, data = df1[df1$Grazing == grazing, ]))
}

ggplot(df1, aes(Root, Fruit)) +
geom_point(aes(colour = Grazing)) +
geom_smooth(method = 'lm') +
geom_smooth(aes(colour = Grazing), method = 'lm')
Edits to this post:
1 result