ggplot inside a function: when does it not recognize arguments and when does it?
ggplot2, r
Solution
Capture the local environment and use it when plotting:
plotfun2 <- function(number) {
localenv <- environment()
ggplot(data = testdata, aes(x = factor(group, levels = c(0:number)), y = value), environment = localenv ) +
geom_boxplot(color = 'red')
}
plotfun2(4)
Problem
Consider the following two functions: ``` library(ggplot2) testdata <- as.data.frame(cbind(rep(c(1, 4), each = 50), rbinom(100, 50, .5))) names(testdata) <- c("group", "value") plotfun1 <- function(color) { ggplot(data = testdata, aes(x = factor(group, levels = c(0: 4)), y = value)) + geom_boxplot(color = color) } plotfun2 <- function(number) { ggplot(data = testdata, aes(x = factor(group, levels = c(0: number)), y = value)) + geom_boxplot(color = 'red') } ``` Plotfun1 works perfectly, calling ``` plotfun1('red') ``` yields two nice red boxplots. However calling ``` plotfun2(4) ``` yields the error message: Error in factor(group, levels = c(0:number)) : object 'number' not found apparently in some cases ggplot is not able to 'find' the arguments of the function, and in some cases it is. What is going on here? PS I know there is an easy work around: ``` plotfun3 <- function(number) { plotdata <- testdata plotdata$group <- factor(plotdata$group, levels = c(0: number)) ggplot(data = plotdata, aes(x = group, y = value)) + geom_boxplot(color = 'red') } ``` I just want to understand what is going on.)