I wonder if somebody knows how to execute this code, but that each level is separately plotted.
sites<-levels(OrchardSprays$treatment)
par(mfcol=c(4,2))
for (i in 1:length(sites)) {
here_tmp<-sites[i]
plot(droplevels(subset(OrchardSprays, Site = here_tmp, select = c(treatment,decrease))))
}
This is the output that it gives me. But I want different graphs of the different levels. I don't know why it gives me the same graphs...
I agree with @SimonG that it's not clear why you'd want to have the each level plotted on a separate graph, but here's a way to do it with ggplot2
, which has a nice system for creating plots of each level of a variable without much extra code:
library(ggplot2)
ggplot(OrchardSprays, aes(treatment, decrease)) +
geom_boxplot() +
facet_wrap(~treatment, scales="free_x", ncol=2)
To put all the boxplots in a single graph, just remove the last line:
ggplot(OrchardSprays, aes(treatment, decrease)) +
geom_boxplot()