0

I am trying to create several plots (using ggplot2) in a loop and I am using below function.

I used the iris data set and added a random factor variable called "ran_fact. So the data set has now four numerical variables and two factors (Species and ran_fac).

At the moment the loop goes through the numerical variables and creates a plot for each. The two factor variables are set manually (as the plots x-axis and color) inside the loop, which makes it less versatile. I would like to know if it is possible to set the x-axis and color option as a function argument and if yes how that can be done?

Thank you in advance.

Also, as a side question, below select_if(x, is.numeric) seems not to remove the factor variables from the data set.

library(datasets)
library(dplyr)
library(ggplot2)
library(beeswarm)

data(iris)
iris$ran_fac <- as.factor(c("A", "B", "C", "D", "E"))

plotFunc <- function(x, na.rm = TRUE,...) {
    plot_list = list()
    nm <- select_if(x, is.numeric)
    nm <- names(x)
    for (i in seq_along(nm)) {
        plots <-ggplot(x,aes_string("ran_fac", nm[i], color="Species")) + geom_quasirandom() + geom_boxplot()
        plot_list[[i]] = plots
        print(plots)
        ggsave(plots,filename=paste("myplot",nm[i],".png",sep=""))
    }
}

plotFunc(iris)

Beeswarm plot

  • Please indent your code properly. I fixed it for you. – Matthew Drury May 07 '17 at 19:46
  • It would help if you could be more explicit about what the desired output is. How exactly do you want to call the function and what do you want the result to be? – MrFlick May 07 '17 at 22:10
  • Where's the example where `select_if(is.numeric)` returns factors? If i run `sapply(iris, class)` I see Species is a factor and if I run `names(dplyr::select_if(iris, is.numeric))` it's not returned. – MrFlick May 07 '17 at 22:13
  • Hi Silke, regarding the 'select_if' question, I think the trouble is that you are trying to retrieve the names of the original data frame (iris, see x in your function) instead of retrieving the names from the subseted data frame (nm). In the function, the select_if function isn't deleting variables from data frame iris but creating a new one with the selected numeric variables. If you use 'nm <- names(nm)', you'll have the names of only the numeric variables. Or you can do it in just one line: 'nm<-names(select_if(x,is.numeric))' – storm surge May 08 '17 at 07:48
  • Of course, @storm surge, that's it. I feel a bit stupid now ;). Thank you. – Silke Johannsen May 08 '17 at 09:01
  • @Matthew Drury Thank you. – Silke Johannsen May 08 '17 at 09:06
  • @MrFlick, I added a link to the graph i am trying to create with above function. (I hope that's what you mean with the desired output) The function works fine as it is. I would just like to avoid having to place the two factors, which I use for my x-axis and as color, inside the function. It would be nice if I could just set the factors at the top before the function runs. – Silke Johannsen May 08 '17 at 09:11

1 Answers1

0

Ok, I found my answer in this Question Passing arguments to ggplot in a wrapper. I adjusted my function and below works as intended. I can now set the x axis and color at the beginning.

library(datasets)
library(dplyr)
library(ggplot2)

data(iris)

iris$ran_fac <- as.factor(c("A", "B", "C", "D", "E"))

xcol <- iris$ran_fac
colorVar <- iris$Species

plotFunc <- function(data, na.rm = TRUE, x, color,...) {
  plot_list = list()
  nm <- select_if(data, is.numeric)
  nm <- names(nm)
  for (i in seq_along(nm)) {
    plots <- ggplot(data = data, aes_string(x = "xcol", nm[i], color = "colorVar")) + geom_quasirandom() + geom_boxplot()
    plot_list[[i]] = plots
    print(plots)
    ggsave(plots, filename = paste("myplot", nm[i],".png", sep=""))

  }
}

plotFunc(iris)
Community
  • 1
  • 1