簡體   English   中英

如何使用在R中作為函數參數提供的變量名來命名圖表的標題?

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

我有一個函數,其唯一目的是生成一個條形圖。 我的數據集叫做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”的圖表。

最終,這就是我想要的圖形。

在此輸入圖像描述

只需使用substitute

labs(title = substitute(system))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM