5

I'm doing a ggpairs plot, but the regression line is too thick and the 'Corr:' text font is too big.

data(mtcars)
head(mtcars)

mtcars$am <- as.factor(mtcars$am)

g <- ggpairs( 
  data = mtcars,
  lower = list(
    continuous = wrap("smooth", alpha = 0.3, color = "blue") 
    )
  )
g <- g + theme(
    axis.text = element_text(size = 6),
    axis.title = element_text(size = 6),
    legend.background = element_rect(fill = "white"),
    panel.grid.major = element_line(colour = NA),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "grey95")
  )
print(g, bottomHeightProportion = 0.5, leftWidthProportion = .5)

This is the output:

enter image description here

I can't find in the GGally documentation where I can set this.

Any pointers?

Chris Snow
  • 23,813
  • 35
  • 144
  • 309

3 Answers3

4

Try this to increase the font size:

data(mtcars)
head(mtcars)

mtcars$am <- as.factor(mtcars$am)

library(ggplot2)
library(GGally)

lowerFn <- function(data, mapping, ...) {
  p <- ggplot(data = data, mapping = mapping) +
    geom_point(color = 'blue', alpha=0.3, size=4) +
    geom_smooth(color = 'black', method='lm', size=1,...)
  p
}

g <- ggpairs( 
  data = mtcars,
  lower = list(
    continuous =  wrap(lowerFn) #wrap("smooth", alpha = 0.3, color = "blue", lwd=1) 
  ),
  upper = list(continuous = wrap("cor", size = 5))
)
g <- g + theme(
  axis.text = element_text(size = 6),
  axis.title = element_text(size = 6),
  legend.background = element_rect(fill = "white"),
  panel.grid.major = element_line(colour = NA),
  panel.grid.minor = element_blank(),
  panel.background = element_rect(fill = "grey95")
)
print(g, bottomHeightProportion = 0.5, leftWidthProportion = .5)

enter image description here

FairMiles
  • 165
  • 9
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
3

How about this?

lowerFn <- function(data, mapping, ...) {
  p <- ggplot(data = data, mapping = mapping) +
    geom_point(color = 'blue', alpha=0.3, size=4) +
    geom_smooth(color = 'black', method='lm', size=1,...)
  p
}

g <- ggpairs( 
  data = mtcars,
  lower = list(
    continuous =  wrap(lowerFn)
  )
)
g <- g + theme(
  axis.text = element_text(size = 6),
  axis.title = element_text(size = 6),
  legend.background = element_rect(fill = "white"),
  panel.grid.major = element_line(colour = NA),
  panel.grid.minor = element_blank(),
  panel.background = element_rect(fill = "grey95")
)
print(g, bottomHeightProportion = 0.5, leftWidthProportion = .5)

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
  • Thanks for this! I've updated the question though to ask how to edit the font size for the 'Corr:' labels. Any ideas? – Chris Snow Sep 26 '16 at 20:47
1

@Chris Snow: use the upper argument of ggpairs function to wrap the ggally_cor function. size = 2 will address your question, however I also added color = "black" in case you want to change the color also. Courtesy: Change colors in ggpairs now that params is deprecated

The modified MWE is:

data(mtcars)
head(mtcars)

mtcars$am <- as.factor(mtcars$am)

g <- ggpairs( 
  data = mtcars,
  lower = list(
    continuous = wrap("smooth", alpha = 0.3, color = "blue") 
    ),
  upper = list(continuous = wrap(ggally_cor, size = 2, color = "black")))
g <- g + theme(
    axis.text = element_text(size = 6),
    axis.title = element_text(size = 6),
    legend.background = element_rect(fill = "white"),
    panel.grid.major = element_line(colour = NA),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "grey95")
  )
print(g, bottomHeightProportion = 0.5, leftWidthProportion = .5)