Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

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

like image 326
Hugo Avatar asked Jan 20 '26 06:01

Hugo


1 Answers

Just use substitute

labs(title = substitute(system))
like image 102
Jilber Urbina Avatar answered Jan 22 '26 22:01

Jilber Urbina