I want to plot a boxplot with the regular /boxplot function of R - not ggplot. Y and X axis are continously numeric varibales (x-axis 6 forces: 1.0, 1.3, 1.6, 2.0, 2.5, 3.1 [N]) On Y-axis the participants ratings (1 to 7).
I would like to plot it, with quantified spacing on x-axis, and also later add regression line into the plot. Cant find anything for the regular /boxplot function.
Code so far:
kraft_ou <- data.frame(VR1_100$ou_kraft,
VR1_125$ou_kraft,
VR1_160$ou_kraft,
VR1_200$ou_kraft,
VR1_250$ou_kraft,
VR1_310$ou_kraft)
colnames(kraft_ou) <- c("kraft_100", "kraft_125", "kraft_160", "kraft_200", "kraft_250", "kraft_310")
kraft_ou
boxplot(kraft_ou,
names=c("1,0 [N]", "1,3 [N]","1,6 [N]","2,0 [N]","2,5 [N]","3,1 [N]"),
col = "bisque",
ylim = c(1, 7))
points(1:6, meanskraftou, pch=4)
text(1:6, meanskraftou 0.24, labels = meanskraftou)
abline(h=4)
data (n=30 ratings from 1 to 7 for each of the 6 forces):
dput(kraft_ou) structure(list(kraft_100 = c(4, 3, 5, 5, 3, 4, 2, 4, 4, 5, 4, 5, 5, 4, 4, 3, 4, 4, 5, 4, 6, 5, 4, 5, 5, 5, 4, 4, 4, 4), kraft_125 = c(4, 4, 5, 6, 4, 3, 4, 4, 4, 5, 4, 5, 4, 5, 4, 3, 4, 4, 4, 6, 6, 4, 4, 5, 3, 5, 4, 4, 4, 5), kraft_160 = c(5, 6, 6, 6, 6, 4, 6, 5, 6, 5, 4, 3, 6, 6, 6, 5, 5, 5, 5, 6, 6, 6, 5, 5, 4, 6, 4, 5, 5, 5), kraft_200 = c(6, 5, 6, 6, 5, 4, 5, 5, 6, 7, 5, 3, 5, 5, 5, 4, 7, 6, 5, 5, 7, 6, 5, 6, 6, 6, 5, 4, 5, 3), kraft_250 = c(5, 6, 6, 7, 6, 6, 6, 6, 7, 7, 6, 5, 7, 7, 5, 5, 6, 6, 7, 7, 6, 6, 5, 5, 5, 7, 4, 6, 6, 5), kraft_310 = c(7, 7, 7, 7, 6, 5, 6, 6, 6, 7, 4, 5, 7, 6, 5, 5, 7, 6, 5, 6, 6, 6, 5, 6, 5, 6, 5, 6, 6, 6)), class = "data.frame", row.names = c(NA, -30L))
CodePudding user response:
You can use the at
argument to specify x locations for your boxplots, though to get them narrow enough to avoid overplotting, you need to add an invisible box and set the relative widths of the visible boxes to a smaller value:
boxplot(cbind(kraft_ou, n = rep(NA, nrow(kraft_ou))),
names=c("1,0 [N]", "1,3 [N]","1,6 [N]","2,0 [N]","2,5 [N]","3,1 [N]",
" "),
col = "bisque",
ylim = c(1, 7), width = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 1),
at = c(1, 1.3, 1.6, 2.0, 2.5, 3.1, 3.1))
abline(h = 4)
To add a regression line, you would need to have all your data frame values in a single y variable, and a vector of their corresponding x axis positions:
abline(lm(unlist(kraft_ou) ~ rep(c(1, 1.3, 1.6, 2.0, 2.5, 3.1), each = 30)))