1

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

我的最后一张图(带有黑色数据点)

另一个生成的图(重叠数据点)

这是我的代码的最后一个版本(通常是 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(color = "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)
    }
4

2 回答 2

1

你总是可以侵入 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
于 2017-11-02T14:25:54.627 回答
0

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

library(mlr)
g = plotLearnerPrediction(makeLearner("classif.randomForest"), iris.task)
g + scale_fill_manual(values = c("yellow", "orange", "red"))
于 2017-10-30T19:43:51.317 回答