由于某些原因,我做了以下几件事:
Fruit <- c(rep("Apple",3),rep("Orange",5))
Bug <- c("worm","spider","spider","worm","worm","worm","worm","spider")
Numbers <- runif(8)
df <- data.frame(Fruit,Bug,Numbers)因子计数
bar.plot <- function(dat,j,c){
ggplot(dat, aes(j, ..count..)) +
geom_bar(aes(fill = c), position = "dodge")
}
bar.plot(df,Fruit,Bug)我得到了
Don't know how to automatically pick scale for object of type function. Defaulting to continuous
Error in eval(expr, envir, enclos) : object 'j' not found我最关心的是关于错误的第二行.有人知道为什么会这样吗?我有很多酒吧情节要做,所以这个功能将使我的生活更容易。
发布于 2014-07-23 17:04:18
由于ggplot的非标准评估,您不能将这样的符号名传递给ggplot函数。直到您实际对aes进行print()处理之后,才会对其进行评估。这就是为什么
bb<-bar.plot(df,Fruit,Bug)不会产生同样的错误。但此时,函数中存在的变量j已经不存在了。如果要动态指定数据列作为aes()表达式中的值,则应使用aes_string。如果您希望能够传递符号名而不是字符串,则可以使用substitute将它们转换为字符。例如,这将起作用。
bar.plot <- function(dat,j,c){
ggplot(dat, aes_string(x=substitute(j)), aes(y=..count..)) +
geom_bar(aes_string(fill = substitute(c)), position = "dodge")
}
bar.plot(df,Fruit,Bug)

https://stackoverflow.com/questions/24916502
复制相似问题