在ggpairs()中调整组文本美学

时间:2018-03-10 19:56:04

标签: r ggally

我正在使用ggpairs()为一组变量创建散点图和相关性。我的相关图上的文字不适合绘图区域,因为它们被水平调整以在绘图的中途结束。我在another post上看到了如何调整背线相关文本,但它不适用于组关联。

以下是一个例子:

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

1 个答案:

答案 0 :(得分:0)

从@jrlewi链接的代码看,答案似乎是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