Hugo
Hugo

Reputation: 347

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.

enter image description here

Upvotes: 3

Views: 238

Answers (1)

Jilber Urbina
Jilber Urbina

Reputation: 61154

Just use substitute

labs(title = substitute(system))

Upvotes: 4

Related Questions