Create multi-density ggplot boxes using application features

I was wondering if it is possible to create a set of boxes similar to the one obtained from these combinations of nested loops using the apply function.

It may not be possible / necessary, but I thought it was possible, I just can’t wrap my head around how to do it.

I need to be able to build this to see how 100s of factors are compared in a single variable ( mtcars$mpg)

head(mtcars)

for (i in 8:11) {
    for (j in 8:11) {

        if (i != j) {

            title = paste(names(mtcars)[i], names(mtcars)[j], 
                sep = "/")

            p <- ggplot(mtcars, aes(interaction(mtcars[,i], mtcars[, j]), mpg, fill = factor(mtcars[,i]))) + geom_boxplot(alpha = I(0.7))
            p <- p + ggtitle(title) + scale_fill_hue()


        } else {

            title = paste(names(mtcars)[i])

            p <- ggplot(mtcars, aes(factor(mtcars[,i]), mpg, fill = factor(mtcars[, i]))) + geom_boxplot(alpha = I(0.7))
            p <- p + ggtitle(title) + scale_fill_hue()


        }

        print(p)
    }
} 
+4
source share
1 answer

Put the if block in the function:

plotGG <- function(i,j) 
  {
  if (i != j) { ... } else{ ... }
 }

Then call it:

mapply(plotGG,8:11,8:11)

And it works.

- ggplot. : aes

EDIT: , :

multiPlotGG <- function(l1,l2) {
 mapply(plotGG,rep(l1,each = length(l2)),rep(l2,length(l1)))
}
multiPlotGG(8:11,8:11)
+3

All Articles