在ggplot2中,我可以自动将标题分成多行,而不使用转义\ n吗? [英] In ggplot2, can I split my titles into multiple lines automatically, instead of using the escape \n?

查看:176
本文介绍了在ggplot2中,我可以自动将标题分成多行,而不使用转义\ n吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个函数,可以为包含数百个问题的大型数据集绘制图形.所有图形都来自同一数据集,并将标题作为字符串从我读过的csv的"graphtitle"列中提取.其中一些标题相当长,应分成两行.也许是因为它们来自csv,所以标题不能通过\n转义起作用,即使这样,标题似乎也应该比将\n添加到适当位置的每个变量名称中更简单的方法.放在CSV中.

有没有办法让我的函数自动分割标题行?我知道我可以更改标题的大小,但这会使它变得难以辨认

MC.Graph <- function(variable){
    p <- ggplot(data = newBEPS, aes_string(x=variable)) + geom_bar(aes(y = (..count..)/sum(..count..)), width = .5)+ scale_y_continuous(labels = percent)
    p <- p + ggtitle(graphwords$graphtitle[graphwords$gw.varname == variable]) + 
    theme(plot.title = element_text(size =20, lineheight = .5, face = "bold"))
}

解决方案

有两种方法可以分解长字符串:

  1. gsub('(.{1,10})(\\s|$)', '\\1\n', s)会将字符串s分解为长度为10的行,并以\n分隔.
  2. strwrap函数与包含paste函数和collapse = \n参数的width参数一起使用.

通过示例更易于理解...

long_title <- "This is a title that is perhaps too long to display nicely"

gsub('(.{1,10})(\\s|$)', '\\1\n', long_title)
# [1] "This is a\ntitle that\nis perhaps\ntoo long\nto display\nnicely\n"

paste(strwrap(long_title, width = 10), collapse = "\n")
# [1] "This is a\ntitle\nthat is\nperhaps\ntoo long\nto\ndisplay\nnicely"

我相信在stringi和/或stringr程序包(请参阅stringi::stri_wrapstringr::str_wrap)中,strwrap的处理更为优美(且猜测效率更高).

I've written a function to make graphs for a large dataset with hundreds of questions. All of the graphs are from the same dataset, and pull the title as a string from the 'graphtitle' column of a csv i've read in. Some of these titles are rather long and should be split to two lines. Perhaps because they're coming from the csv, the titles don't function with an \n escape, and even if they did, it seems like there should be an easier way than adding \n to each variable name in the appropriate place in the CSV.

Is there a way to make my function split the title lines automatically? I know I could change the size of the title, but that will render it largely illegible

MC.Graph <- function(variable){
    p <- ggplot(data = newBEPS, aes_string(x=variable)) + geom_bar(aes(y = (..count..)/sum(..count..)), width = .5)+ scale_y_continuous(labels = percent)
    p <- p + ggtitle(graphwords$graphtitle[graphwords$gw.varname == variable]) + 
    theme(plot.title = element_text(size =20, lineheight = .5, face = "bold"))
}

解决方案

Here are two ways to break up a long string:

  1. gsub('(.{1,10})(\\s|$)', '\\1\n', s) will break up string, s, into lines of length 10 separated by \n.
  2. Use the strwrap function with a width argument wrapped with a paste function and collapse = \n argument.

Easier to understand with examples...

long_title <- "This is a title that is perhaps too long to display nicely"

gsub('(.{1,10})(\\s|$)', '\\1\n', long_title)
# [1] "This is a\ntitle that\nis perhaps\ntoo long\nto display\nnicely\n"

paste(strwrap(long_title, width = 10), collapse = "\n")
# [1] "This is a\ntitle\nthat is\nperhaps\ntoo long\nto\ndisplay\nnicely"

N.B. I believe strwrap is handled more gracefully (and guessing efficiently) in the stringi and/or stringr packages (see stringi::stri_wrap or stringr::str_wrap).

这篇关于在ggplot2中,我可以自动将标题分成多行,而不使用转义\ n吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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