`stat_bin()` using `bins = 30`. Pick better value `binwidth`.
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.
Add aesthetics to the panels
Next, we note that we can add mappings (aesthetics) to the panels, e.g. to color by a variable.
Specifying the mapping as above modifies all the panels in the pairs plot. We can also customize individual panels. Here we illustrate this by coloring the points in the panels below the diagonal and adding a regression line for each group, but displaying only the overall correlation in the panels above the diagonal.
Code
## Define plot function for the scatter plotsplotpoints <-function(data, mapping, ...) {ggplot(data = data, mapping = mapping) +aes(color = transmission) +geom_smooth(method ="lm", se =FALSE, formula ="y ~ x",linetype ="dashed") +geom_point(alpha =0.5, size =3) +theme_bw(13)}## Use this function in the ggpairs callggpairs(mycars, lower =list(continuous = plotpoints),columns =c("mpg", "disp", "hp", "wt"),progress =FALSE) +theme_bw(13) +labs(title ="A subset of the mtcars data set",subtitle ="Lines represent linear regression")
We can also modify the panels above the diagonal to display the overall correlation, but change the font size and add a background color according to the strength of the correlation.
Code
## Define correlation function for the panels above the diagonalcor_fcn <-function(data, mapping, ...) {## Get data xData <- GGally::eval_data_col(data, mapping$x) yData <- GGally::eval_data_col(data, mapping$y)## Calculate correlation corr <-cor(xData, yData, method ="pearson")## Define background color## The colorRamp() function creates a function that map the interval ## [0, 1] to colorsif (corr >=0) { cols <-hcl.colors(n =11, palette ="RdBu")[6:2] col <-rgb(colorRamp(cols)(abs(corr)),maxColorValue =255) } else { cols <-hcl.colors(n =11, palette ="RdBu")[6:10] col <-rgb(colorRamp(cols)(abs(corr)),maxColorValue =255) }## Construct plotggplot(data = data, mapping = mapping) +annotate(x =0.5, y =0.5, label =paste0("Corr: ", round(corr, digits =3)),geom ="text",size =abs(corr) *5+1) +theme_bw(13) +xlim(c(0, 1)) +ylim(c(0, 1)) +theme(panel.background =element_rect(fill = col),panel.grid.major =element_blank(),panel.grid.minor =element_blank())}ggpairs(mycars, lower =list(continuous = plotpoints),upper =list(continuous = cor_fcn),columns =c("mpg", "disp", "hp", "wt"),progress =FALSE) +labs(title ="A subset of the mtcars data set",subtitle ="Lines represent linear regression")
Remarks
It is possible to define customized plots separately for continuous, discrete and ‘combo’ data types, see the ggpairs documentation for more details.