Coloring points in a pairs plot

前端 未结 2 614
挽巷
挽巷 2021-02-08 22:37

I would like to color points in a pairs plot based of certain row indexes. Here is the code I used for plotting 1 variable against another.

cases<-which(rown         


        
2条回答
  •  孤街浪徒
    2021-02-08 23:10

    I'm not sure if @Roland 's answer works in some version, but at least in my Windows R 3.4.2, it doesn't.

    The function pairs takes many arguments. Some of this are used to indicate what function to map to the diagonal, upper and lower panels. By default, it uses the plot (points) function.

    This function has a parameter bg used to specify the fill color of markers that take it, like pch = 21.

    Also, the color mapping can be done much more efficiently with unclass. For example, with a two-levels factor variable:

    colors <- c('black', 'red')[unclass(factor_variable)]
    

    Then, this does the magic:

    pairs(data, bg=colors)
    

提交回复
热议问题