带表达式的ggplot2双行标签

met*_*oia 53 r ggplot2

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

例:

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))
Run Code Online (Sandbox Code Playgroud)

产生一个图像,其中下标"报告"在我希望它位于前一个单词旁边时向右踢出. ggplot2带有表达式的两行标签

Dre*_*een 57

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

Gavin Simpson提到的解决方法是:

#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]))))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

p + xlab(expression(paste("line1 \n line2 a" [b])))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

p + xlab(expression(paste("abc \n abcd" [reported])))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

p + xlab(expression(paste("abc \n ab" [reported])))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

下标总是最终与上线右端的右侧对齐.

p + xlab(expression(paste("abcdefghijklmnop \n ab" [reported])))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 很好的示范.不知道如何处理下标,但是第二行的*start*稍微向右移动的原因是因为换行符之后有一个额外的空格.输入p + xlab(表达式(paste("abcdefghijklmnop \nab"[报告])))而不是p + xlab(表达式(paste("abcdefghijklmnop \n ab"[报告])))至少确保两行*开始*在相同的位置......虽然不可否认这并没有绕过下标之前的难看的差距. (2认同)

bap*_*ste 11

你可以用这个技巧,

library(gridExtra)
library(grid)

element_custom <- function() {
  structure(list(), class = c("element_custom", "element_text"))
}

element_grob.element_custom <- function(element, label="", ...)  {

  mytheme <- ttheme_minimal(core = list(fg_params = list(parse=TRUE, 
                                                         hjust=0, x=0.1)))
  disect <- strsplit(label, "\\n")[[1]]
  tableGrob(as.matrix(disect), theme=mytheme)
}

# default method is unreliable
heightDetails.gtable <- function(x) sum(x$heights)

ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_line() + 
  labs(x= "First~line \n italic('and a second') \n integral(f(x)*dx, a, b)")+
  (theme_grey() %+replace% theme(axis.title.x = element_custom()))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Mau*_*ers 8

该软件包ggtext提供了一个不同的选项,允许 HTML 标签格式化/自定义标签和文本。

library(ggtext)
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  xlab("A long string of text goes here just for the purpose<br>of illustrating my point Weight<sub>reported</sub>") +
  theme(axis.title.x = element_markdown())
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Val*_*tin 5

1)解决方案 cowplot::draw_label()

一个人也可以使用draw_label()包中的注释功能cowplot(在讨论中建议)。我们可以调用cowplot::draw_label()尽可能多的文本行。当cowplot::draw_label()与结合使用时cowplot::ggdraw(),它可以在画布/图纸上的任何位置注释,坐标范围为0到1(相对于整个画布)。

需要调整注释位置并为自定义轴标题留出足够的空间。

请注意,cowplot包目前改变默认ggplot主题,因此,如果需要的话,使用theme_set()所提到加载包后在这里

还要注意的是,功能cowplot::draw_label()用途的ggplot2::annotation_custom()引擎盖下。我将在下面的第二部分中提到更多。

library(ggplot2)
library(cowplot)
#> 
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave

# If needed, revert to default theme (cowplot modifies the theme); 
# theme_set(theme_grey())

p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
# Make enough space for the custom two lines axis title
p <- p + 
  xlab("") + # empty label
  # Tweak the margins (push the label down by forcing a wider top margin)
  theme(axis.title.x = element_text(size = 10, # also adjust text size if needed
                                    margin = margin(t = 10, r = 0, b = 0, l = 0,
                                                    unit = "mm")))

# The two lines we wish on the plot
line_1 <- "A long string of text for the purpose"
line_2 <- expression(paste("of illustrating my point" [reported]))
# Or avoid paste() (is not actually needed)
# line_2 <- expression("of illustrating my point" [reported])

# Call cowplot::draw_label two times to plot two lines of text
ggdraw(p) + 
  draw_label(line_1, x = 0.55, y = 0.075) + # use relative coordinates for positioning
  draw_label(line_2, x = 0.55, y = 0.025)
Run Code Online (Sandbox Code Playgroud)

请注意,cowplot::draw_label()也可以与设置剪裁关闭结合使用coord_cartesian(clip = "off"),以允许在画布上的任何位置进行绘图。这次我们不再使用相对坐标,而是使用绘图/数据中的相对坐标(绝对坐标):

# Other two expressions
line_1b <- expression(bolditalic('First line'))
line_2b <- expression(integral(f(x)*dx, a, b))

p + coord_cartesian(clip = "off") + # allows plotting anywhere on the canvas
  draw_label(line_1b, x = 3.5, y = 8.2) + # use absolute coordinates for positioning
  draw_label(line_2b, x = 3.5, y = 6)
Run Code Online (Sandbox Code Playgroud)

reprex软件包(v0.2.1)创建于2019-01-14


2)解决方案 ggplot2::annotation_custom()

如前所述,cowplot::draw_label()是的包装ggplot2::annotation_custom()。因此,相反cowplot::draw_label(),我们可以直接ggplot2::annotation_custom()与设置剪裁功能结合使用- coord_cartesian(clip = "off")合并此拉取请求就可以使用剪裁功能。

但是,这种方法比较冗长,带有更多的坐标参数,因此我们需要使用grid::textGrob()

# Some other two lines we wish on the plot as OX axis title
line_1c <- expression("Various fonts:" ~ bolditalic("bolditalic") ~ bold("bold") ~ italic("italic"))
line_2c <- expression("this" ~~ sqrt(x, y) ~~ "or this" ~~ sum(x[i], i==1, n) ~~ "math expression")
# the ~~ ads a bit more space than ~ between the expression's components

p + coord_cartesian(clip = "off") +
  annotation_custom(grid::textGrob(line_1c), xmin = 3.5, xmax = 3.5, ymin = 7.3, ymax = 7.3) +
  annotation_custom(grid::textGrob(line_2c), xmin = 3.5, xmax = 3.5, ymin = 5.5, ymax = 5.5)
Run Code Online (Sandbox Code Playgroud)

reprex软件包(v0.2.1)创建于2019-01-14