在完成对数据框的操作后,生成ggplot()
图时出现意外问题。我提供一个说明性的例子:由于var.name
引述ggplot处理带引号的变量
func <- function(){
library(ggplot2)
df <- read.table(....)
# perform operation on df to retrieve column of interest index/number
column.index <- regexpr(...)
# now need to find variable name for this column
var.name <- names(df)[column.index]
# also need to mutate data in this column
df[,column.index] <- df[,column.index] * 10
# generate plot
plot <- ggplot(data, aes(x=var.name))+geom_bar()
print(plot)
}
这里ggplot将抛出一个错误,例如,“省油”。 任何想法如何解决这个问题?
编辑:从this question测试解决方案无济于事。
@ David:我在NSE中测试了'quote',所以我添加了标签以提示有关问题的性质。 – remi
Gotcha。快速打字和思考,想到我把它:-) – remi