ggplot错误"至少一个图层必须包含用于刻面的所有变量"

时间:2018-05-25 12:00:21

标签: r facet-wrap

我在R markdown中的代码块中定义了一个函数。目标是创建由RACE变量构成的折线图。

{r, Helper function}
  line_graph_gender_race <- 
    function(data_new, xnew, ynew, titlenew, colornew, lwdnew, ylab, wrap) {
      line_graph_gender_race <- 
          ggplot(data = data_new,aes(x = xnew,y = ynew, color = colornew)) + 
          geom_line(lwd = lwdnew) + 
          labs(title = titlenew, y = ylab) + 
          facet_wrap(~wrap, nrow = 1, ncol = 2) + 
          theme(axis.text.x = element_text(angle = 45, vjust = 0.5))
      return (line_graph_gender_race)
    }

接下来,我们将这个函数称为另一个块;

{r, ref.label="Helper function"}

line_graph_gender_race(
  population_gender_race1_2,
  population_gender_race1_2$YEAR, 
  population_gender_race1_2$TOTAL_POP, 
  "Projected Population as Per Gender from July 1, 2012 through July 1,2060\n(White vs. Black Population)", 
  population_gender_race1_2$SEX, 
  2,  
  "Population Percent", 
  population_gender_race1_2$RACE
)

此分面变量RACE存在。但我们看到一个错误:

  

至少一个图层必须包含用于刻面的所有变量

我该如何解决这个问题?

0 个答案:

没有答案