Reputation: 2443
> levels(iris$Species)
[1] "setosa" "versicolor" "virginica"
I plan to use above names as xlab
in scripts as below:
aggregate(Sepal.Length~Species,iris,function(x){
car::qqPlot(x,distribution="norm",xlab="names(x)")
})
After qqPlot
,I found xlab
not resolved.
The general questions is:
How to wrap factor level name
in aggregate
function like aggregate(y~factor,df,function(x){factor level name})
?
Upvotes: 1
Views: 61
Reputation: 76402
Here are two ways, I believe the question asks for the second one. None of th two needs aggregate
.
1. The first way is to use the formula interface directly. It puts all graphs in the same page/plot.
car::qqPlot(Sepal.Length ~ Species, data = iris, distribution = "norm")
2. The second way splits the data set by the levels of the factor and then lapply
's an anonymous function to plot each QQ plot separately.
sp <- split(iris, iris$Species)
lapply(seq_along(sp), function(i){
sp[[i]]$Species <- droplevels(sp[[i]]$Species)
car::qqPlot(Sepal.Length ~ Species, data = sp[[i]],
distribution = "norm", xlab = names(sp)[i])
})
rm(sp) # final clean up
Upvotes: 1