r - ggpairs : Colouring the geoms by a Factor -


i trying replicate ggpairs can accomplished in pairs -- i.e. colour geoms (e.g. scatter points etc) binary factor (which represents class). can not. below reproducible example using dataset smarket library islr:

library(islr) data(smarket) pairs(smarket, col = smarket$direction) 

this returns following graph:

enter image description here

now, using ggpairs without specifying colour class following plot:

smarket$class <- ifelse(smarket$direction == "up", 1, -1)  ggpairs(smarket %>% select(-9) ,          lower = list(continuous = wrap("points", color = "red", alpha = 0.5),                       combo = wrap("box", color = "blue", alpha = 0.3),                       discrete = wrap("facetbar", color = "orange", alpha = 0.3) ),          diag = list(continuous = wrap("densitydiag",  color = "yellow", alpha = 0.5) )) 

enter image description here

but attempts colour geoms class fail:

> color_by_class <- as.factor(ifelse(smarket$direction == "up", "red", "black")) > ggpairs(smarket %>% select(-9) ,  +         lower = list(continuous = wrap("points", color = color_by_class, alpha = 0.5),  +                      combo = wrap("box", color = "orange", alpha = 0.3),  +                      discrete = wrap("facetbar", color = color_by_class, alpha = 0.3) ),  +         diag = list(continuous = wrap("densitydiag",  color = color_by_class, alpha = 0.5) )) error: aesthetics must either length 1 or same data (512): colour, alpha > length(color_by_class) [1] 1250 > dim(smarket %>% select(-9)) [1] 1250    8 

or

> ggpairs(smarket %>% select(-9) ,  +         lower = list(continuous = wrap("points", aes(color = color_by_class), alpha = 0.5),  +                      combo = wrap("box", aes(color = color_by_class), alpha = 0.3),  +                      discrete = wrap("facetbar", aes(color = color_by_class), alpha = 0.3) ),  +         diag = list(continuous = wrap("densitydiag",  aes(color = color_by_class), alpha = 0.5) )) error in wrap("points", aes(color = color_by_class), alpha = 0.5) :    parameters must named arguements 

your advice appreciated.

@ jdb

thank elegant solution. however, when copy , paste code , run getting error (see below). me understand why?

> library(dplyr) > library(ggplot2) > library(islr) > library(ggally) > data(smarket) >  > smarket$class <- ifelse(smarket$direction == "up", 1, -1) > smarket$class <- as.factor(smarket$class) >  > ggpairs(smarket %>% select(-9), aes(colour = class, alpha = 0.4)) error in fun(x[[i]], ...) :    defined on data frame numeric variables in addition: warning message: `panel.margin` deprecated. please use `panel.spacing` property instead  

@ jdb

thank again. removed ggally , reinstalled using latest binary file cran. worked.

you can add standard ggplot aesthetics ggpairs, converting class variable factor , using colour aesthetic should trick.

library(dplyr) library(ggplot2) library(islr) library(ggally) data(smarket)  smarket$class <- ifelse(smarket$direction == "up", 1, -1) smarket$class <- as.factor(smarket$class)  ggpairs(smarket %>% select(-9), aes(colour = class, alpha = 0.4)) 

enter image description here


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -