简体   繁体   中英

How to name a graph's title using the variable name supplied to as function argument in R?

I have a function whose only objective is to produce a bar graph. My dataset is called 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 = ________________)  
}
# ......................................................................................

How do I get my graph's title to be string I supplied to my function's argument system ?

If I try labs(title = system) , I get a graph whose title is "C".

Ultimately, that's what I want my graph to look like.

在此输入图像描述

只需使用substitute

labs(title = substitute(system))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM