繁体   English   中英

在qplot中循环cut2 color参数

[英]Looping cut2 color argument in qplot

首先是公平的警告,它与Coursera.org实用机器学习中的测验问题有关。 但是,我的问题并不涉及实际提出的问题,而是关于绘图的切线问题。

我有一组训练有素的数据,我试图为每个预测变量创建一个绘图,其中包括y轴上的结果,x轴上的数据集的索引,并通过预测变量对绘图进行着色,以便确定导致指标出现偏差的原因。 为了使color参数更加清晰,我尝试使用Hmisc包中的cut2()

这是我的数据:

library(ggplot2)
library(caret)
library(AppliedPredictiveModeling)
library(Hmisc)
data(concrete)
set.seed(1000)
inTrain = createDataPartition(mixtures$CompressiveStrength, p = 3/4)[[1]]
training = mixtures[ inTrain,]
testing = mixtures[-inTrain,]
training$index <- 1:nrow(training)

我试过了,可以绘制所有图,但是它们都是相同的颜色。

plotCols <- function(x) { 
  cols <- names(x)
  for (i in 1:length(cols)) {
    assign(paste0("cutEx",i), cut2(x[ ,i]))
    print(qplot(x$index, x$CompressiveStrength, color=paste0("cutEx",i)))
  }
}
plotCols(training)

然后,我尝试了此方法,并绘制了所有图,这一次它们是彩色的,但剪切不起作用。

plotCols <- function(x) { 
  cols <- names(x)
  for (i in 1:length(cols)) {
    assign(cols[i], cut2(x[ ,i]))
    print(qplot(x$index, x$CompressiveStrength, color=x[ ,cols[i]]))
  }
}
plotCols(training)

似乎qplot()不喜欢color参数中包含paste() 有谁知道另一种循环遍历color参数并仍然保持剪切的方法? 任何帮助是极大的赞赏!

使用ggplot()而不是qplot()可以更轻松地实现所需的输出,因为您可以使用aes_string() ,它接受字符串作为参数。

plotCols <- function(x) { 
  cols <- names(x)
  for (i in 1:length(cols)) {
    assign(paste0("cutEx", i), cut2(x[, i]))

    p <- ggplot(x) +
         aes_string("index", "CompressiveStrength", color = paste0("cutEx", i)) +
         geom_point()

    print(p)
  }
}

plotCols(training)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM