I have defined a function in a code chunk in R markdown. The goal is to create a line graph facetted by RACE
variable.
{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)
}
Next, we call this function in a different chunk;
{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
)
This facetting variable RACE
is present. But we see an error:
At least one layer must contain all variables used for facetting
How can I solve this?