Search code examples
rcol

Color/fill one point differently in a scatterplot


I am using base R graphics to make a scatterplot with thousands of data points. One point out of these has the highest 'y' value. I want to fill this point to make it look different. In the past I have accomplished this with one of the following. Of course then the number of points were very few and thus I was able to manage it easily. Now I have ~3000 points. Any ideas?

col=c{'black','black','black','red','black','black'}
pch=c(16,16,16,17,16,16)

Solution

  • Option 1: Identify the max and make your color vector

    set.seed(47)
    n <- 1e4
    xx <- runif(n)
    yy <- rexp(n)
    colors <- rep("black", n)
    colors[which.max(yy)] <- "red"
    plot(xx, yy, col = colors, pch = 16)
    

    Option 2: Plot the max separately. This is probably easier, especially if you want to adjust more characteristics than just color.

    plot(xx, yy, pch = 16)
    points(xx[which.max(yy)], yy[which.max(yy)], col = "red", pch = 17, cex = 2)