2

I have a longitudinal data which I pass to ggplot with facet_wrap and a subsetting of the dataframe. I wish to 'function'-alize this and I am running into trouble. I have seen similar posts to this, but not one with facet_wrap and data subsetting inside the function. For example, I have used information from this post in the past to do simple graphs. Below I show the section of code for generating dummy data and then plotting the graphic. That works OK. It's when I try to use the function call that I get the error message:

Your help is greatly appreciated. Thank you!!

Error: Faceting variables must have at least one value

# Test

# Generate data
DF <- expand.grid(Time=(0:10), variable=as.factor(c("ux0", "ux1", "ux2")), model=as.factor(c("Model 1", "Model 2", "Model 3")))
DF$value <- DF$Time + rnorm(nrow(DF), 0, 20)

# load libraries
library(ggplot2)
library(scales)

# Define themes
My_Theme = theme_bw()+
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        aspect.ratio = 1,
        axis.title=element_text(size=7),
        axis.text.x=element_text(size=rel(0.6)),
        axis.text.y=element_text(size=rel(0.6)),
        strip.text = element_text(size = 6))

#Plot
my.plot  =  
  ggplot(subset(DF, variable %in% "ux1")) +
  geom_line(aes(x=Time, y=value)) +
  facet_wrap( ~ model, ncol=3) + 
  labs(x = "Time [s]", y = expression(paste("U"[X],","[1]))) +
  My_Theme
print(my.plot)

#Now try with function
makePlots <- function(data, subSetVar, subSetval, xVar, yVar, facetVar, 
                      xLabel, yLabel){

  # Common Theme to all plots

  My_Theme = theme_bw()+
    theme(panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank(),
          aspect.ratio = 1,
          axis.title=element_text(size=7),
          axis.text.x=element_text(size=rel(0.6)),
          axis.text.y=element_text(size=rel(0.6)),
          strip.text = element_text(size = 6))

  my.plot  =  
    ggplot(subset(data, subSetVar %in% subSetval)) + 
    geom_line(aes(x=xVar, y=yVar)) + 
    facet_wrap(facetVar, ncol=3) + 
    labs(x = xLabel, y = yLabel) +
    My_Theme


  # Output to Plots window in RStudio
  print(my.plot)

}

my.plot <- makePlots(DF, "variable", "ux1", "Time", "value", "model",
                     "Time [s]", expression(paste("U"[X],","[1])))

'''

1 Answer 1

3

In order to pass character strings as variable into ggplot, you need to make some changes in my.plot part before wrapping it into a function.

For subset the dataset, you need to pass the names of the column with [[ ]] in order to make it work. For the definition of x and y, you can use aes_string (https://ggplot2.tidyverse.org/reference/aes_.html). Finally, for facetting, pass your character vector as a formula (as explained in this post: Passing string variable facet_wrap() in ggplot using R).

my.plot  =  
    ggplot(subset(data, data[[subSetVar]] %in% subSetval)) + 
    geom_line(aes_string(x=xVar, y=yVar)) +
    facet_wrap(as.formula(paste("~", facetVar)), ncol=3) + 
    labs(x = xLabel, y = yLabel) +
    My_Theme

Then, it should work and get you the same graph :

my.plot <- makePlots(DF, "variable", "ux1", "Time", "value", "model",
                     "Time [s]", expression(paste("U"[X],","[1])))

enter image description here

Does it answer your question ?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.