R 从全局环境中访问直接传递给ggplot包装器中函数的对象
我正在为R 从全局环境中访问直接传递给ggplot包装器中函数的对象,r,function,ggplot2,environment,scoping,R,Function,Ggplot2,Environment,Scoping,我正在为ggplot2生成一个基本包装,以生成直方图。因为我需要生成大量的图形,所以我更容易使用一个函数来遍历所有的变量并输出所需的图形。我的函数代码如下所示: # Libs require(ggplot2); require(ggthemes); require(grid) GenerateHistogram <- function(histogramVariable, dataSet, graphTitle = "Graph
ggplot2
生成一个基本包装,以生成直方图。因为我需要生成大量的图形,所以我更容易使用一个函数来遍历所有的变量并输出所需的图形。我的函数代码如下所示:
# Libs
require(ggplot2); require(ggthemes); require(grid)
GenerateHistogram <- function(histogramVariable, dataSet,
graphTitle = "Graph Title",
xAxis = "Count",
yAxis = "x axis title") {
# Get the histogram value as indicator
histVar <- get(paste(deparse(substitute(dataSet)), histogramVariable,
sep = "$"), envir = parent.frame(),
inherits = TRUE)
# Plot definition
hist_plot <- ggplot(data = dataSet, aes_string(x = histogramVariable)) +
geom_histogram() +
scale_y_continuous(expand = c(0,0)) +
geom_vline(aes(xintercept = mean(histVar)), colour = 'red',
size = 1) +
ggtitle(graphTitle) +
xlab(xAxis) +
ylab(yAxis) +
annotate("text", x = mean(histVar)*1.8,
y = mean(histVar) - mean(histVar) * 0.1,
label = paste("Mean:",round(mean(histVar),0)),
colour = 'red') +
theme_gdocs() +
scale_colour_gdocs() +
theme(axis.title.y = element_text(angle = 90),
plot.margin = unit(c(5,5,5,5),"mm"))
# Return
return(hist_plot)
}
我收到以下错误:
问题似乎出在以下陈述中:
histVar <- get(paste(deparse(substitute(dataSet)), histogramVariable,
sep = "$"), envir = parent.frame(),
inherits = TRUE)
这似乎产生了错误:
平均值错误(histVar):未找到对象“histVar”
我认为有多种方法可以解决这个问题,而不涉及范围界定。这里有一个,我在函数中生成了一个包含平均值的附加数据帧,并将其传递给geom_vline。也许有一种更优雅的方法可以做到这一点,但这种方法给了你很多控制和一种自己计算的方法(更少的黑匣子) 我已经删除了您的所有附加格式,以专注于解决方案
GenerateHistogram <- function(histogramVariable="disp", dataSet=mtcars,
graphTitle = "Graph Title",
xAxis = "Count",
yAxis = "x axis title") {
#generate additional/summarizing data
#this gives you a dataframe you can feed to geom_vline,
#so more control and no scoping issues
add_data <- data.frame(mean=mean(dataSet[,histogramVariable]))
# Plot definition
hist_plot <- ggplot(data = dataSet, aes_string(x = histogramVariable)) +
geom_histogram() +
geom_vline(data=add_data, aes(xintercept=mean))
# Return
return(hist_plot)
}
数据集和变量已在函数环境中。有没有什么原因让你不去做
dataset[,historogramvariable]
,而不是你目前采用的get from parent environment
-方法?谢谢你在我的帖子中表现出兴趣。我猜问题在于如何传递变量。对于aes\u字符串
而言,变量作为变量
传递,而不是作为变量
传递,如果我做dataset[,historagramvariable]
它将是dataset[,“historagramvariable”]
,我想这不会起作用,或者我遗漏了什么东西?[我正在处理它]。数据帧列可以通过字符串(df[,“a”])访问,如果变量是字符串,也可以通过变量访问。因此,如果您想使用b@Heroka,这可能是一个解决方案,而不是在get
上瞎折腾,环境和范围规则非常感谢您的贡献。我将进行实验,如果可行,为什么不让它回答呢?非常感谢,聪明的解决方案!不客气。我添加了一个编辑,因为我有“这应该是没有额外数据的可能”的挫折感。非常感谢,非常有用的解决方案。我可能会在调整垃圾箱的宽度和制作一些其他的小玩意儿上花点功夫,所以我不介意额外的数据。
histVar <- get(paste(deparse(substitute(dataSet)), histogramVariable,
sep = "$"), envir = parent.frame(),
inherits = TRUE)
# Get the histogram value as indicator
relevant_column <- histogramVariable
histVar <- dataSet[,relevant_column]
GenerateHistogram <- function(histogramVariable="disp", dataSet=mtcars,
graphTitle = "Graph Title",
xAxis = "Count",
yAxis = "x axis title") {
#generate additional/summarizing data
#this gives you a dataframe you can feed to geom_vline,
#so more control and no scoping issues
add_data <- data.frame(mean=mean(dataSet[,histogramVariable]))
# Plot definition
hist_plot <- ggplot(data = dataSet, aes_string(x = histogramVariable)) +
geom_histogram() +
geom_vline(data=add_data, aes(xintercept=mean))
# Return
return(hist_plot)
}
geom_vline(aes_string(xintercept=sprintf("mean(%s)",histogramVariable)))