Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:根据输入设置ggplot标题的写入函数_R_Function_Ggplot2_Axis Labels - Fatal编程技术网

R:根据输入设置ggplot标题的写入函数

R:根据输入设置ggplot标题的写入函数,r,function,ggplot2,axis-labels,R,Function,Ggplot2,Axis Labels,我正在尝试创建一个函数,使我能够绘制一系列数据帧。我试图将绘图标题设置为一个变量,该变量将根据指示风险因素的变量进行更改 这是riskf(原始数据框)为吸烟和二手烟暴露设置的数据框 smoking添加带有键的向量: plot_function <- function(risks){ riskf <- gsub("()","",sys.call()[2]) titles <- c(smoking="Smoking by Province in Canada 2014"

我正在尝试创建一个函数,使我能够绘制一系列数据帧。我试图将绘图标题设置为一个变量,该变量将根据指示风险因素的变量进行更改

这是riskf(原始数据框)为吸烟和二手烟暴露设置的数据框


smoking添加带有键的向量:

plot_function <- function(risks){

  riskf  <- gsub("()","",sys.call()[2])
  titles <- c(smoking="Smoking by Province in Canada 2014",
              second_smoking="Second Hand Smoke by Province in Canada 2014")

  plot <- ggplot(risks, aes(provinces, value )) +  geom_bar(aes(fill = variable), 
  position = "dodge", stat="identity") + scale_fill_discrete(name="Gender") + 
  xlab("Provinces and Territories") + ylab("Percentage(%)") + 
  theme_bw() + theme(panel.border = element_blank()) +
  theme(plot.title = element_text(size=20), axis.title.x = element_text(size=14), 
  axis.title.y = element_text(size=14)) + geom_hline(yintercept=mean(risks[, 3]))+
  ggtitle(titles[riskf])

  return(plot)
}

plot\u函数您不能添加另一个参数用于标题吗?R怎么知道把“二手烟”变成“二手烟”?您是否计划在某个地方定义这些翻译?
plot_function <- function(risks){

  riskf  <- gsub("()","",sys.call()[2])
  titles <- c(smoking="Smoking by Province in Canada 2014",
              second_smoking="Second Hand Smoke by Province in Canada 2014")

  plot <- ggplot(risks, aes(provinces, value )) +  geom_bar(aes(fill = variable), 
  position = "dodge", stat="identity") + scale_fill_discrete(name="Gender") + 
  xlab("Provinces and Territories") + ylab("Percentage(%)") + 
  theme_bw() + theme(panel.border = element_blank()) +
  theme(plot.title = element_text(size=20), axis.title.x = element_text(size=14), 
  axis.title.y = element_text(size=14)) + geom_hline(yintercept=mean(risks[, 3]))+
  ggtitle(titles[riskf])

  return(plot)
}
plot_function <- function(x) {
  riskf  <- gsub("()","",sys.call()[2])
  titles <- c(smoking="Smoking by Province in Canada 2014",
              second_smoking="Second Hand Smoke by Province in Canada 2014")

  print(titles[riskf])
}

smoking <- 1:3

plot_function(smoking)
#                              smoking 
# "Smoking by Province in Canada 2014"