2

To increase the readability of a pair plot, with many variables, I would like to round the coefficient of correlation provided by ggpairs() function.

In the following example, instead of having a coefficient of 0.807 displayed, I would like to have 0.8.

library(GGally)

data(cars)

ggpairs(cars,
  upper = list(continuous = wrap(ggally_cor, alignPercent = 0.8, size = 10)))

enter image description here

P. Denelle
  • 790
  • 10
  • 24
  • 1
    Possible duplicated: [link](https://stackoverflow.com/questions/47496364/rounding-digits-in-ggpairs) – bbiasi May 02 '19 at 14:33

2 Answers2

5

You can add digits=2 in the ggpairs() call for 2 digit r coefficient.

The updated ggpairs() would be like that;

ggpairs(cars,
        upper = list(continuous = wrap(ggally_cor, alignPercent = 0.8, digits=2, size = 10)))

enter image description here

I hope it will be helpful.

washfaq
  • 268
  • 2
  • 12
0

Instead of calling for ggally cor, call for text using a predefined correlation where you've already rounded. That way the text is the rounded coefficient.

more tweaking found here: link

ct <- cor.test(cars$speed,cars$dist)
r <- ct$estimate
rt <- format(r, digits=2)  #can have 1 instead of 2
 # just demonstrating that .807 becomes .81

ggpairs(cars,
        upper = list(continuous = function(data, mapping, ...) {
          ggally_text(label = as.character(rt), mapping = mapping)}), 
        lower = list(continuous = 'smooth'), 
        axisLabels = "internal"
)
Jonni
  • 804
  • 5
  • 16