67

我想用一条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))

产生一个图像,当我希望它坐在前一个单词旁边时,下标“reported”被踢到右边。 ggplot2 带有表达式的两行标签

4

4 回答 4

70

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

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

在此处输入图像描述

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

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

在此处输入图像描述

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

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

在此处输入图像描述

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

在此处输入图像描述

下标总是与上线右端的右侧对齐。

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

在此处输入图像描述

于 2012-11-05T02:31:19.080 回答
14

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)

请注意,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)

reprex 包(v0.2.1)于 2019 年 1 月 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)

reprex 包(v0.2.1)于 2019 年 1 月 14 日创建

于 2019-01-14T15:03:43.120 回答
13

你可以用这个技巧,

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

在此处输入图像描述

于 2015-08-29T00:37:02.857 回答
9

该包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())

在此处输入图像描述

于 2020-03-06T02:35:30.430 回答