0

I have a data frame dat that I am trying to plot with a for loop. The reason why I'm using a forloop in a function to plot ggplots is because I would like to call this function many times later.

> head(dat)
      tpl     motif strand base score ipdRatio
1:  24501 AAGTACTCG      0    A    51    3.108
2:  58809 GAGTACTAC      0    A    69    4.095
3:  65614 TAGTACTCA      0    A    61    3.341
4:  78494 GAGTACTAC      0    A    92    4.968
5:  92127 AAGTACTTA      0    A    23    1.702
6: 193102 GAGTACTCG      0    A    96    5.255

I continue getting an error:

Error in eval(as.symbol(x_val)) : error in evaluating the argument 'expr' in selecting a method for function 'eval': Error in as.symbol(x_val) : object 'x_val' not found

When I try calling the function like so:

plotme <- function(dataf,x_val,bin_width){
  print(ggplot(dataf, aes(x = eval(as.symbol(x_val)))) +
          geom_histogram(binwidth = bin_width))
}

ratioplot <- plotme(dat,"ipdRatio",.5)

Any suggestions on what might be causing the error here?

4
  • 1
    Anything you put inside aes() should exist in the data.frame you feed ggplot() with. Commented Aug 14, 2015 at 7:17
  • but column ipdRatio exists in dat dataframe Commented Aug 14, 2015 at 7:18
  • But not x_val, as said in the error message. Commented Aug 14, 2015 at 7:18
  • Can x_val not be variable that I can read into the function, because I'm calling it in aes()? I ask this because I'm planning to create histograms with x values at different columns. Commented Aug 14, 2015 at 7:19

2 Answers 2

1

I was able to find a solution, I simply had to use aes_string instead of aes to call in variable aes column values.

Like so:

plotme <- function(dataf,x_val,bin_width){
  print(ggplot(dataf, aes_string(x = x_val)) +
          geom_histogram(binwidth = bin_width))
}

ratioplot <- plotme(dat,"ipdRatio",15)
Sign up to request clarification or add additional context in comments.

1 Comment

You can go ahead and check this to indicate the question has been answered.
0

First of, I prefer the aes() over aes_string and then just do

if (is.character(x_val)) {x_val <- which(names(dataf) == x_val)}

That way you can call it as numeric or as character when refering to the column.

Secondly, there is a something wrong with the function you have made.

try and clear your workspeace entirely, and run this this:

dat <- data.frame(ipdRatio=rnorm(100));library(ggplot2)

plotme <- function(dataf,x_val,bin_width){
        localEnv <- environment()
        if (is.character(x_val)) {x_val <- which(names(dataf) == x_val)}
        print(ggplot(dataf, aes(x = dataf[,x_val])) +
                      geom_histogram(binwidth = bin_width))
}

ratioplot <- plotme(dataf=dat,"ipdRatio",.5)

would return

Error in eval(expr, envir, enclos) : object 'dataf' not found

The function call dataf=dat is simply not being made, and the reason it works for you now is because you have dataf in your global environment.

to change the function to specify the environment as local

dat <- data.frame(ipdRatio=rnorm(100));

plotme <- function(dataf,x_val,bin_width){
        localEnv <- environment()
        if (is.character(x_val)) {x_val <- which(names(dataf) == x_val)}
        print(ggplot(dataf, aes(x = dataf[,x_val]),environment=localEnv) +
                      geom_histogram(binwidth = bin_width))
}

ratioplot <- plotme(dataf=dat,"ipdRatio",.5)

2 Comments

Your original function and the function in @Chani's answer run fine for me without needing the addition of an environment argument. And I definitely don't have anything named datadf floating around my environment...
dataf - okay maybe it is just my specific error, but with a clean global environment, and without specifiying env in the function i get an error.

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.