在ggplot2中,我可以自动将标题分成多行,而不使用转义\ n吗? [英] In ggplot2, can I split my titles into multiple lines automatically, instead of using the escape \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"))
}
有两种方法可以分解长字符串:
-
gsub('(.{1,10})(\\s|$)', '\\1\n', s)
会将字符串s
分解为长度为10
的行,并以\n
分隔. - 将
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_wrap
或stringr::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:
gsub('(.{1,10})(\\s|$)', '\\1\n', s)
will break up string,s
, into lines of length10
separated by\n
.- Use the
strwrap
function with awidth
argument wrapped with apaste
function andcollapse = \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屋!