Facet Grid通过向量中的变量循环

时间:2019-07-01 22:52:51

标签: r ggplot2

我正在制作图形,并且想使用facet_grid函数基于变量进行分离。我正在使用循环绘制图形,因此我想根据自己所处的循环进行构面。

这是我正在尝试的方法,但没有多方面:

  SQL_Table %>%
          filter(iss_age == user_issue_age) %>%
          ggplot(aes_string("dur", "ext_perk_compare",color =column.names2[for.num])) +
          geom_smooth(se = TRUE) +
          annotate("rect",xmin = 0, xmax = x_max_shade,ymin = y_min_shade,
                   ymax = y_max_shade, alpha = .2) +
          facet_grid(.~ column.names2[for.num]) +
          ggtitle(paste('Individual Extension Rates by ', column.names2[for.num],' a Percent of "',
                        base.value,'" - IA',user_issue_age, sep = "")) +
          xlab("Policy Duration") +
          ylab(paste('Premium Rate as a % of ',base.value, sep = "")) +
          labs(color= paste(column.names2[for.num],' group', sep = "")) +
          theme(plot.title = element_text(face = "bold"))

这是专门由于以下代码而引起的:

facet_grid(.~ column.names2[for.num])

column.names2看起来像这样:

> column.names2
[1] "plan"           "gender"         "marital_status" "acceleration"   "extension"      "inflation" 

例如,当for.num = 1时,希望我的代码像这样:

facet_grid(.~ plan)

并且当for.num = 2

facet_grid(.~ gender)

我该怎么做?

0 个答案:

没有答案