嵌套循环遍历每个因子并在r / ggplot中生成每个图的组合

时间:2016-07-19 06:50:31

标签: r ggplot2

我正在尝试使用ggplot生成多个绘图,这些绘图会在单个数据框中针对响应(作为y轴)耗尽每个因子组合(如x轴和颜色)。一个小警告是,我想将分类因素作为因子和序数因素作为数字绘制(所以我基​​本上排序了我的列名数组末尾的所有数字列 - 我在for循环中有一些丑陋的逻辑处理它)

这是我到目前为止所做的代码:

    library(ggplot2)
    df <- mpg


    column_names<- names(df)

    #categorical factors: "manufacturer", "model", "cyl", "trans", "drv"  
    categoricalFactors <- c(column_names[1],column_names[2], column_names[5], column_names[6], column_names[7])

    #ordinal factors: "displ", "year"
    numericalFactors <- c(column_names[3],column_names[4])

    #combines above such that the numerical factors are at the end, starting from index 6
    all_factors <- c(categoricalFactors,numericalFactors)

    #responses: "cty", "hwy"
    responses <- c(column_names[8],column_names[9])

    for (i in 1:length(responses)){ #iterate through all the responses
      for(j in 1:length(all_factors)){ #iterate through all the factors (for y-axis)
        for(k in 1:length(all_factors)){ #iterate through all the factors (for color or lty or group)

          if(j!=k){ #just to make sure I didn't plot the y-axis and color together
            if(j<6 & k<6){ #indicates j and k are both categorial, use as.factor for both
              ggplot(df, aes(as.factor(all_factors[j]), responses[i], color=as.factor(all_factors[k]))) + geom_line()
              #also tried:
              #ggplot(df, aes(as.factor(df[,all_factors[j]]), df[,responses[i]], color=as.factor(df[,all_factors[k]]))) + geom_line()
            }
            else if(j<6 & k>=6){ #indicates j is categorical (as.factor) and k is ordinal (as.numeric)
              ggplot(df, aes(as.factor(all_factors[j]), responses[i], color=as.as.numeric(as.character((all_factors[k]))))) + geom_line()
            }
            else if(j>=6 & k<6){#indicates j is ordinal (as.numeric) and k is categorical (as.factor)
              ggplot(df, aes(as.numeric(as.character((all_factors[j]))), responses[i], color=as.factor(all_factors[k]))) + geom_line()
            }
            else if(j>=6 & k>=6){#indicates j and k are both ordinal  (as.numeric)
              ggplot(df, aes(as.numeric(as.character(all_factors[j])), responses[i], color=as.numeric(as.character(all_factors[k])))) + geom_line()
            }

          }
        }
      }
    }

我尝试通过将其指定为df [,all_factors [1]]来引用该列,但它并不适用于迭代的ggplot。例如:

    ggplot(df, aes(as.factor(df[,all_factors[j]]), df[,responses[i]], color=as.factor(df[,all_factors[k]]))) + geom_line()

没有绘制任何东西。但是添加文字缩进并在控制台中尝试命令确实有效:

    >>ggplot(df, aes(as.factor(df[,all_factors[1]]), df[,responses[1]], color=as.factor(df[,all_factors[2]]))) + geom_line()

编辑:想出我应该在每个ggplot行之前添加一个print语句

第二个问题仍然存在:是否有更优雅的方式来完成这项工作?

0 个答案:

没有答案