3

I am using ggpairs() to create scatterplots and correlations for a set of variables. The text on my correlation plots don't fit onto the plot area, as they are horizontally adjusted to end halfway through the plot. I saw on another post how to adjust the topline correlation text, but it didn't work for the group correlations.

Here's an example:

n = 1000
test_results = tibble(
  test1=sample(1:10, n, replace=TRUE),
  test2=sample(1:10, n, replace=TRUE),
  test3=sample(1:10, n, replace=TRUE),
  test4=sample(1:10, n, replace=TRUE),
  test5=sample(1:10, n, replace=TRUE),
  political=sample(c("Democrat", "Republican", "Green", "Libertarian"), n, replace=TRUE))

test_results %>% 
  select(test1, test2, test3, test4, test5, political) %>%
  ggpairs(columns=c(1:5), mapping = aes(color = political, alpha=.5), 
          upper = list(continuous = wrap("cor", size = 3, hjust=0))
          )

Group correlation text off of image

  • this might help you? https://stackoverflow.com/questions/47496364/rounding-digits-in-ggpairs – jrlewi Mar 10 '18 at 20:21
  • Not sure if I get this straight, but do you mean that the political party names that you use in the sample code are actually descriptions a lot longer; in fact so long that the first part of each description is cut off on the left side of each plot area? – 4rj4n Mar 10 '18 at 21:27
  • @4rj4n Yup that's exactly the problem. The words "Democrat", and "Republican" etc. are getting cut off on the left. If I can get the text aligned on the right, I think they'll fit. – Chetan Sharma Mar 10 '18 at 22:47
  • @jrlewi it sounds like I should write a custom correlation function with a different variable than xP for the ggally_text? I can try that – Chetan Sharma Mar 10 '18 at 22:49
  • You can always divide a long name/description over a second line by introducing an line break/enter somewhere in the string. This will cause it to be divided over two lines in the output, too. It does however mess up alignment. Not sure whether your alignPercent answer below takes care of that properly. – 4rj4n Mar 11 '18 at 08:24

1 Answers1

3

From the code that @jrlewi linked, it looks like the answer is alignPercent!

test_results %>% 
  select(test1, test2, test3, test4, test5, political) %>%
  ggpairs(columns=c(1:5), mapping = aes(color = political, alpha=.5), 
          upper = list(continuous = wrap("cor", size = 3, hjust=0.15, alignPercent=1))
          )

Properly aligned text