如何使用R中作为函数参数提供的变量名来命名图形标题?

时间:2019-08-02 14:55:31

标签: r ggplot2

我有一个函数,其唯一目的是生成条形图。我的数据集称为south

# ......................................................................................
# Data for my example 
south = matrix(data = sample(x = c("A","B","C"), size = 1032, replace = T), ncol = 12)
# ......................................................................................
# Function to graph the count of 'A', 'B', and 'C' from 'data'
graphMaker = function(system){
system %>%
  as.data.frame() %>%
  gather(key = "Key", value = "Values") %>% 
  ggplot(aes(Values, fill = Values)) +
  geom_bar() +
  labs(title = ________________)  
}
# ......................................................................................
  

如何获取图形标题为提供给函数的字符串   参数system

如果我尝试labs(title = system),则会得到一个标题为“ C”的图表。

最终,这就是我希望图形显示的样子。

enter image description here

1 个答案:

答案 0 :(得分:4)

只需使用substitute

labs(title = substitute(system))