rnso
rnso

Reputation: 24535

ggplot not finding object in aes

I have following dataframe and function:

structure(list(vnum1 = c(-0.853002701467605, -2.41044831451325, 
                          1.22391941685926, 0.539011835935724, 
                         -1.33616761309235, -1.33762097068431, 
                          0.0391687995434752, -0.0396899713936502, 
                          -1.34004495967791, 0.731212395958092), 

                vnum2 = c(-0.0140296461607895, 0.649714762844125, 
                          -0.202655014660386, 1.90785563726907, 
                           0.240191747220876, 0.0395243104031934, 
                           -2.1689146110194, -0.198126356757339, 
                           1.89172814288286, -0.484592561521101), 
             vint1 = c(7L, 4L, 7L, 3L, 10L, 10L, 7L, 8L, 2L, 3L), 
             vint2 = c(2L, 8L, 2L, 7L, 3L, 3L, 2L, 8L, 4L, 6L), 
             vfac1 = structure(c(3L, 1L, 2L, 4L, 1L, 1L, 2L, 2L, 1L, 2L),
            .Label = c("1", "2", "3", "4"), class = "factor"), 
             vfac2 = structure(c(3L, 4L, 4L, 2L, 4L, 1L, 1L, 2L, 1L, 4L), 
            .Label = c("1", "2", "3", "4"), class = "factor"), 
             vch1 = structure(c(5L,  1L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 5L), 
            .Label = c("A", "B", "C", "D", "E"), class = "factor"), 
             vbin1 = structure(c(1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L), 
            .Label = c("a", "b"), class = "factor")), 
            .Names = c("vnum1", "vnum2", "vint1", "vint2", "vfac1", "vfac2", "vch1", "vbin1"), 
            row.names = c(NA, -10L), class = "data.frame")
> 

grfour1 <- function(gdf, first, second, third, fourth){
    ggplot(gdf, aes(first, second)) + 
        geom_point(position = position_jitter(width = 0.2, height = 0)) + 
        facet_grid(third~fourth) 
}

On running the command, I get this error:

> grfour1(rndf, vint1, vint2,vch1,vint2)
Error in eval(expr, envir, enclos) : object 'second' not found
> 

Where is the error? Thanks for helping.

Upvotes: 0

Views: 543

Answers (3)

lukeA
lukeA

Reputation: 54237

grfour1 <- function(gdf, first, second, third, fourth){
  ggplot(gdf, aes_string(deparse(substitute(first)), deparse(substitute(second)))) + 
    geom_point(position = position_jitter(width = 0.2, height = 0)) +
    facet_grid(deparse(substitute(third~fourth))) 
}

should work with grfour1(rndf, vint1, vint2,vch1,vint2), if you called e.g. attach(rndf) before. Explanation see @ilir & @DavidArenburg.

Upvotes: 0

David Arenburg
David Arenburg

Reputation: 92282

There are several issues here. First, ggplot parses column names into the aes without the quotes, thus when you do aes(first, second), ggplot2 actually looks for columns named "first" and "second" rather the variables that contains the names you parsed. Second problem is that R doesn't know whats vints, he thinks it's some object in the global enviroment, thus, you need to tell it that this is a character using "vint1"

My solution will be

grfour1 <- function(gdf, first, second, third, fourth){
    gdf <- gdf[c(first, second, third, fourth)]
    names(gdf) <- c("first", "second", "third", "fourth") 
    ggplot(gdf, aes(first, second)) + 
      geom_point(position = position_jitter(width = 0.2, height = 0)) + 
      facet_grid(third ~ fourth) 
  }
library(ggplot2)
grfour1(rndf, "vint1", "vint2", "vch1", "vint2")

Upvotes: 1

ilir
ilir

Reputation: 3224

Your column names are only visible to the ggplot function because it uses the data as its environment. They are not visible to R in your global environment. To fix the problem your function definition should be:

grfour1 <- function(gdf, first, second, third, fourth){
    ggplot(gdf, aes_string(first, second)) + 
      geom_point(position = position_jitter(width = 0.2, height = 0)) + 
      facet_grid(paste(third, "~", fourth)) 
}

In this case first to fourth should be character variables:

grfour1(rndf, "vint1", "vint2", "vch1", "vint2")

Upvotes: 0

Related Questions