1
votes

I have produced some nice plots with the plotLearnerPrediction function of the MLR package. I was able to make some adjustments to the returned ggplot (see my code below). But I am not sure how to make the last adjustment. Namely, I want to change the coloring of the data points based on labels (groups in example plot).

My last plot (with black data points)

Another produced plot (overlapping data points)

This is the last version of my code (normally part of a for loop):

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))

Below is a part of the source code of the plotLearnerPrediction function. I want to overrule geom_point(colour = "black"). Adding simply geom_point(colour = "pink") to my code will not color data points, but the whole plot. Is there a solution to overrule that code with a vector of colors? Possibly a change in the aes() is also needed to change colors based on groups.

        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)
    }
2

2 Answers

1
votes

You can always hack into gg objects. The following works for ggplot2 2.2.1 and adds a manual alpha value to all geom_point layers.

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
0
votes

The plotLearnerPrediction() function returns the ggplot plot object, which allows for some level of customization without having to modify the source code. In your particular case, you can use scale_fill_manual() to set custom fill colors:

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