Skip to content
Prev 351250 / 398503 Next

R CMD methods and ggplot2 advice

Dear Glenn,

Suppose this function

test <- function(df){
  ggplot(df, aes(x = gp, y = y)) + geom_point()
}

Then R CMD check will consider gp and y as global variables since they are
undefined. Because R CMD check cannot detect that gp and y will be
extracted from df by ggplot2.

Possible workarounds

# now gp and y are defined within the function. ggplot2 still looks for gp
and y in df.
test <- function(df){
  gp <- NULL
  y <- NULL
  ggplot(df, aes(x = gp, y = y)) + geom_point()
}

# now "gp" and "y" are strings and hence defined
test <- function(df){
  ggplot(df, aes_string(x = "gp", y = "y")) + geom_point()
}

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2015-05-26 2:24 GMT+02:00 Glenn Schultz <glennmschultz at me.com>: