ggplot2 带表达式的两行标签 [英] ggplot2 two-line label with expression

查看:36
本文介绍了ggplot2 带表达式的两行标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 expression() 语句在两行上写一个轴标签.但是,plotmathexpression 不允许这样做(例如下标文本出现在最右侧).我发现 这个讨论 大约在 2005 年有类似问题,但他们提供的解决方法不会转化为我在 ggplot2 中的应用程序.最近的一个问题解决了多行表达式语句的不同排列,但再次提供了解决方法不适用于此处.

I'd like to write an axis label over two lines with an expression() statement. However, plotmath and expression won't allow this (e.g. subscripted text appears on the far right). I found this discussion circa 2005 of a similar issue but the work around that they offer doesn't translate to my application in ggplot2. A recent question addressed a different permutation of multi-line expression statements, but again the work around provided doesn't apply here.

示例:

p <- ggplot(mtcars,aes(x=wt,y=mpg))+
  geom_point()+
  xlab(expression(paste("A long string of text goes here just for the purpose 
 of illustrating my point Weight "[reported])))
try(ggsave(plot=p,filename=<some file>,height=4,width=6))

产生一个图像,当我希望它位于前一个单词旁边时,下标reported"被踢出右侧.

yields an image where subscript "reported" is kicked out to the right when I'd like it to sit next to the previous word.

推荐答案

我认为这是一个错误.(或者是不支持多行表达式"这一事实的结果,如您链接到的对话中所述).

I think this is a bug. (Or a consequence of the fact that "multi-line expressions are not supported", as stated in the conversation you linked to).

Gavin Simpson 提到的解决方法是:

The workaround that Gavin Simpson alluded to is:

#For convenience redefine p as the unlabeled plot
p <- ggplot(mtcars,aes(x=wt,y=mpg))+geom_point()

#Use atop to fake a line break
p + xlab(expression(atop("A long string of text for the purpose", paste("of illustrating my point" [reported]))))

可以使用带有下标的真正换行符.在下面的简短示例中,与您的示例具有相同的形式,下标正确放置在文本的其余部分旁边,但两行文本未正确居中:

It is possible to use true line breaks with subscripts. In the short example below, which has the same form as your example, the subscript is correctly placed adjacent to the rest of the text but the two lines of text are not centered correctly:

p + xlab(expression(paste("line1 
 line2 a" [b])))

我认为在这两种情况下,当上一行文本比下一行文本长时,下标放置错误.比较

I think that in both cases, the subscript is placed wrong when the upper line of text is longer than the lower line of text. Compare

p + xlab(expression(paste("abc 
 abcd" [reported])))

p + xlab(expression(paste("abc 
 ab" [reported])))

下标总是最后对齐到上一行右端的右侧.

The subscript always ends up aligned just to the right of the right end of the upper line.

p + xlab(expression(paste("abcdefghijklmnop 
 ab" [reported])))

这篇关于ggplot2 带表达式的两行标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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