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

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

问题描述

我想用表达式()语句在两行中写一个轴标签。然而, plotmath 表达式不允许这样做(例如,下标文本出现在最右侧)。大约在2005年,我发现这个讨论类似的问题,但他们提供的工作并没有转化为我在ggplot2中的应用。 最近的一个问题解决了多行表达式陈述的不同排列,但是再次提供了解决方法在这里不适用。



示例:

  p < -  ggplot(mtcars,aes(x = wt,y = mpg))+ 
geom_point()+
xlab(表达式(粘贴(一长串文本仅用于此目的\ n ($)
try(ggsave(plot = p,filename =< some file>,height = 4,width = 6))

会生成一个图像,其中,当我希望它位于前一个单词的旁边时,将已报告的下标向右踢出。

解决方案

我认为这是一个错误。 (或者是你不支持多行表达式这一事实的后果,正如你所链接的对话所述)。



Gavin Simpson提到的解决方法为了方便,将p重新定义为未标记的图
p < - ggplot(mtcars,aes(x = wt,y = mpg))+ geom_point()

#用于伪造换行符
p + xlab(expression(atop(A long string of the text for the purpose,paste (说明我的观点[报告]))))



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

  p + xlab(表达式(paste(line1 \\\
line2 a[b])))



我认为在这两种情况下,当文本的上面一行比文本的下面一行更长时,下标会出错。比较

  p + xlab(表达式(paste(abc \ abcd[reported])))

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


下标总是以上排右端右侧对齐为准。

  p + xlab(表达式(paste(abcdefghijklmnop \\\
ab[reported])))


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.

Example:

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

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).

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 \n 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 \n abcd" [reported])))

p + xlab(expression(paste("abc \n 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 \n ab" [reported])))

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

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