Here's minimal working problem. Consider the following data frame
DF <- data.frame(
y=1:10,
x=10:1,
z=rep(5,10),
a=11:20
)
Assume y
is my response. Now, for every pair plot (y,_)
in ggpairs(DF)
I want to mark point 1, 5 and 10 with a different color. How do I do this?
There doesn't seem to be an in-built way of doing this in ggpairs
. There is a way round it, which is to modify the plot it produces. Of necessity, this is a "hacky" solution. It first requires you to specify the points panels (i.e. the ones you wish to modify), and of course the rows whose points you wish to be red:
point_panels <- c(5, 9, 10, 11, 13:15)
red_points <- c(1, 5, 10)
If you have done that correctly, the following code should transform your plots appropriately:
ggp <- ggpairs(DF)
ggp$data$colors <- "normal"
ggp$data$colors[red_points] <- "special"
for(i in point_panels) {
ggp$plots[[i]]$mapping <-
`class<-`(c(ggp$plots[[i]]$mapping, aes(color = colors)), "uneval")
}
ggp <- ggp + scale_color_manual(values = c("black", "red"))
So now when we do:
ggp
we get
If you want to add a legend, do
ggp$legend <- point_panels[1]
ggp