在R中以ggpairs包装列名称文本 [英] Wrap column name text in ggpairs in R

查看:147
本文介绍了在R中以ggpairs包装列名称文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ggpairs,在绘制矩阵时,我收到如下矩阵

I am using ggpairs and while plotting the matrix, I receive a matrix as follows

如您所见,某些文本长度很大,因此看不到完整的文本.无论如何,我是否可以包装文本以便使其完全可见.

As you can see, some of the text length is large and hence the text is not seen completely. Is there anyway that I can wrap the text so that it is visible completely.

代码

ggpairs(df) 

我希望文字能自动换行,以便可以看到类似这样的文字

I want the text to wrap so that it can be seen something like this

推荐答案

您可以使用ggpairslabeller自变量传递要应用于小平面条文本的函数.

You can use the labeller argument of ggpairs to pass a function to be applied to the facet strip text.

ggplot确实有一个不错的现成函数label_wrap_gen(),用于包装长标签.

ggplot does have a nice ready function label_wrap_gen() that wrap the long labels.

默认情况下,ggpairs使用列名作为标签,并且不能包含空格. label_wrap_gen()需要空格以将标签分成多行.

By default ggpairs use the column names as labels, and those can't contain spaces. label_wrap_gen() need spaces to split the labels on multiple rows.

这是一个解决方案:

library(ggplot2)
library(GGally)
df <- iris

colnames(df) <- make.names(c('Long colname', 
                  'Quite long colname', 
                  'Longer tha usual colname',
                  'I\'m not even sure this should be a colname',
                  'The ever longest colname that one should be allowed to use'))

ggpairs(df, 
        columnLabels = gsub('.', ' ', colnames(df), fixed = T), 
        labeller = label_wrap_gen(10))

这篇关于在R中以ggpairs包装列名称文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆