在完成對數據框的操作後,生成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