1

I have a data frame with 8 variables, I tried to plot bar plots of 3 variables(discrete variables) in one page using the following script:

lyst <- list(colnames(mydata[3:5]))
pl<-lapply(lyst, function(i)ggplot(mydata, aes_string(i)) + geom_bar(aes_string(fill=i)))
marrangeGrob(pl, nrow=2, ncol=2)

At first it worked, but now I get the following error message: Error: More than one expression parsed Call rlang::last_error() to see a backtrace.

rlang::last_error() More than one expression parsed Backtrace:

  1. base::lapply(...)

  2. global::FUN(X[[i]], ...)

  3. ggplot2::aes_string(i)

  4. base::lapply(...)

  5. ggplot2:::FUN(X[[i]], ...)

  6. rlang::parse_expr(x).

Any one have an idea how to fix this error?

markus
  • 25,843
  • 5
  • 39
  • 58
jiyuna ben
  • 11
  • 3
  • Could you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? For example, if you provide part of you data, I can run the code on my machine and help you :D – luizgg Dec 06 '19 at 21:29

1 Answers1

0

For me, the column names of my dataframe contained unwanted characters like semicolons and white spaces. To fix this problem, I cleaned the column names before passing them as strings to aes_string(). You can do this with base r's make.names() function or using janitor::clean_names() before running your code.

Solution:

# Using base r:

colnames(mydata) <- make.names(colnames(mydata))

# Using Janitor:

install.packages(janitor) # If not already installed

mydata <- janitor::clean_names(mydata)

tamtam
  • 3,541
  • 1
  • 7
  • 21