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()
Your problem seems to be with how you use subset
. First, there is no variable named Site
in the data set. Second, Site = here_tmp
is not a logical expression.
Assuming you meant treatment
instead of Site
, try this:
sites <- levels(OrchardSprays$treatment)
par(mfcol=c(4,2))
for (i in 1:length(sites)) {
here_tmp<-sites[i]
plot(droplevels(subset(OrchardSprays, treatment == here_tmp,
select = c(treatment,decrease))))
}
I'm not really sure though if this is your desired outcome because, personally, I don't see a reason to put them in separate plots.
Let me know if this was helpful!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.