更改颜色数据点 plotLearnerPrediction(MLR 包)

change color data points plotLearnerPrediction (MLR package)

我用 MLR 包的 plotLearnerPrediction 函数制作了一些不错的图。我能够对返回的 ggplot 进行一些调整(请参阅下面的代码)。但我不确定如何进行最后的调整。也就是说,我想根据标签(示例图中的组)更改数据点的颜色。

My last plot (with black data points)

Another produced plot (overlapping data points)

这是我的代码的最后一个版本(通常是 for 循环的一部分):

plot <- plotLearnerPrediction(learner = learner_name, task = tasks[[i]], cv = 0,
                              pointsize = 1.5, gridsize = 500) + 
  ggtitle(trimws(sprintf("Predictions %s %s", meta$name[i], meta$nr[i])), 
          subtitle = sprintf("DR = %s, ML = %s, CV =  LOO, ACC = %.2f", meta$type[i], 
                             toupper(strsplit(learner_name, "classif.")[[1]][2]), acc[[i]])) + 
  xlab(sprintf("%s 1", lab)) + 
  ylab(sprintf("%s 2", lab)) + 
  scale_fill_manual(values = colors) +
  theme(plot.title = element_text(size = 18, face = "bold"),
        plot.subtitle = element_text(size = 12, face = "bold", colour = "grey40"),
        axis.text.x = element_text(vjust = 0.5, hjust = 1),
        axis.text = element_text(size = 14, face = "bold"),
        axis.title.x = element_text(vjust = 0.5),
        axis.title = element_text(size = 16, face = "bold"),
        #panel.grid.minor = element_line(colour = "grey80"),
        axis.line.x = element_line(color = "black", size = 1),
        axis.line.y = element_line(color = "black", size = 1),
        panel.grid.major = element_line(colour = "grey80"),
        panel.background = element_rect(fill = "white"),
        legend.justification = "top",
        legend.margin = margin(l = 0),
        legend.title = element_blank(),
        legend.text = element_text(size = 14))

以下是plotLearnerPrediction函数的部分源代码。我想否决 geom_point(colour = "black")。简单地向我的代码添加 geom_point(colour = "pink") 不会为数据点着色,而是为整个图着色。有没有一种解决方案可以用颜色矢量否决该代码?可能还需要更改 aes() 以根据组更改颜色。

        else if (taskdim == 2L) {
        p = ggplot(mapping = aes_string(x = x1n, y = x2n))
        p = p + geom_tile(data = grid, mapping = aes_string(fill = target))
        p = p + scale_fill_gradient2(low = bg.cols[1L], mid = bg.cols[2L], 
            high = bg.cols[3L], space = "Lab")
        p = p + geom_point(data = data, mapping = aes_string(x = x1n, 
            y = x2n, colour = target), size = pointsize)
        p = p + geom_point(data = data, mapping = aes_string(x = x1n, 
            y = x2n), size = pointsize, colour = "black", 
            shape = 1)
        p = p + scale_colour_gradient2(low = bg.cols[1L], 
            mid = bg.cols[2L], high = bg.cols[3L], space = "Lab")
        p = p + guides(colour = FALSE)
    }

plotLearnerPrediction() 函数 returns ggplot 绘图对象,它允许进行某种程度的自定义,而无需修改源代码。在您的特定情况下,您可以使用 scale_fill_manual() 设置自定义填充颜色:

library(mlr)
g = plotLearnerPrediction(makeLearner("classif.randomForest"), iris.task)
g + scale_fill_manual(values = c("yellow", "orange", "red"))

你总是可以破解 gg 对象。以下适用于 ggplot2 2.2.1 并向所有 geom_point 层添加手动 alpha 值。

library(mlr)
library(ggplot2)
g = plotLearnerPrediction(makeLearner("classif.qda"), iris.task)
ids.geom.point = which(sapply(g$layers, function(z) class(z$geom)[[1]]) == "GeomPoint")
for(i in ids.geom.point) {
  g$layers[[i]]$aes_params$alpha = 0.1
}
g