包装长的geom_text标签 [英] wrapping long geom_text labels

查看:42
本文介绍了包装长的geom_text标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ggplot2中包装长文本时遇到问题.在这里

在这种情况下似乎不起作用!

解决方案

使用Unicode零宽度空间,并将所有 _ 替换为 _ +:

 库(stringi)库(ggplot2)my_label<-"Some_arbitrarily_larger_text"my_label<-stri_replace_all_fixed(my_label,"_","_ \ U200B") 

肉眼可见,它是连续的:

  my_label## [1]任意_大文本" 

但是,以编程方式,这是一个断断续续的机会:

 包装器<-函数(x,...)paste(stri_wrap(x,...),折叠="\ n")ggplot(mtcars,aes(x = wt,y = mpg))+geom_point()+注释(文本",x = 4,y = 25,标签=包装器(my_label,宽度= 5)) 

I have an issue with wrapping long texts in ggplot2. Similar question was asked here ggplot2 is there an easy way to wrap annotation text?

My question is if we have the text like this

my_label <- "Some_arbitrarily_larger_text"

How can we shrink it using the same method ?

wrapper <- function(x, ...) paste(strwrap(x, ...), collapse = "\n")

library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()+
annotate("text", x = 4, y = 25, label = wrapper(my_label, width = 5))

It seems not working for this case!

解决方案

Use a Unicode zero-width space and substitute all the _'s with _ + it:

library(stringi)
library(ggplot2)

my_label <- "Some_arbitrarily_larger_text"
my_label <- stri_replace_all_fixed(my_label, "_", "_\U200B")

To the naked eye it's continuous:

my_label
## [1] "Some_​arbitrarily_​larger_​text"

But, programmatically, it's a word break/wrap opportunity:

wrapper <- function(x, ...) paste(stri_wrap(x, ...), collapse = "\n")

ggplot(mtcars, aes(x = wt, y = mpg)) + 
  geom_point() +
  annotate("text", x = 4, y = 25, label = wrapper(my_label, width = 5))

这篇关于包装长的geom_text标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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