rf7
rf7

Reputation: 2191

Improving the readability of the scatterplot in ggpairs, ggplot

I am using the Auto dataset from the ISLR library and the function ggpairs() from gpairs library to create a scatterplot of all possible combinations of variables. My code is the following:

data(Auto)
setDT(Auto)
ggpairs(Auto[, -c("name"), with = FALSE] , 
        lower = list(continuous = wrap("points", color = "red", alpha = 0.5), 
                     combo = wrap("box", color = "orange", alpha = 0.3), 
                     discrete = wrap("facetbar", color = "yellow", alpha = 0.3) ), 
                    diag = list(continuous = wrap("densityDiag",  color = "blue", alpha = 0.5) ))+
     theme(axis.text.x = element_text(angle = 90, hjust = 1))

The plot is the one below: enter image description here

There are some issues with this plot:

  1. The axes tick labels are not readable. How could I remove the numbers and possibly rotate the tick lables to be vertical to the axes?

  2. How could I enforce different colors for the combo pairs (categorical - continuous)

Your advice will be appreciated.

Upvotes: 1

Views: 3204

Answers (2)

timfaber
timfaber

Reputation: 2070

Very tricky stuff, the comb wrapper uses factor levels as input to change colors (as shown in the example by @KoenV) and not discrete which is a bit of a misnomer. Changing the class however changes the upper/diag part.

The only alternative I found was manually changing plots using putPlot.

# remove ticks (I don't get labels when plotting it)

p = ggpairs(Auto[, -c("name"), with = FALSE] , 
        lower = list(continuous = wrap("points", color = "red", alpha = 0.5), 
                     combo = wrap("box", color = "orange", alpha = 0.3), 
                     discrete = wrap("facetbar", color = "yellow", alpha = 0.3)), 
        diag = list(continuous = wrap("densityDiag",  color = "blue", alpha = 0.5) ))+
        theme(axis.text = element_blank(),axis.ticks = element_blank())

And change a individual plot and adding a boxplot/ + color (perhaps loop over the desired plots

cp = ggplot(Auto) +
  geom_boxplot(aes(as.factor(cylinders), mpg),color="orange")
putPlot(p, cp, 2,1)

Upvotes: 0

KoenV
KoenV

Reputation: 4283

Maybe the proposed solution is not a perfect match with your wishes, but I hope it helps.

  1. You need to invoke more libraries to get the code to work.
  2. You will need to have factors to "force" the categorical variables to be known as such.

The following code may do the trick:

library(ISLR)
library(data.table)
library(GGally)
library(ggplot2)
data(Auto, package = "ISLR")

# remove unwanted column and make categorical variables
Auto2 <- Auto[, -9]
Auto2$cylinders <- factor(Auto2$cylinders)
Auto2$origin <- factor(Auto2$origin)

ggpairs(Auto2 , 
        lower = list(continuous = wrap("points", color = "red", alpha = 0.5), 
                     combo = wrap("box", color = "orange", alpha = 0.3), 
                     discrete = wrap("facetbar", color = "yellow", alpha = 0.3) ), 
        diag = list(continuous = wrap("densityDiag",  color = "blue", alpha = 0.5) ))

This yields the following picture:

enter image description here

Please let me know whether this is what you want.

Upvotes: 2

Related Questions