GGPLOT2两线标签与表达(ggplot2 two-line label with express

2019-06-18 20:07发布

我想写在两行轴标签与expression()语句。 然而, plotmathexpression不会允许(出现在最右边如标文本)这一点。 我发现这个讨论大约一个类似的问题,但解决办法,他们提供的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))

产生下标“报道”被踢出的权利时,我想它旁边坐到前一个单词的图像。

Answer 1:

我认为这是一个错误。 (或者说“多行表达式不支持”,在您链接到谈话指出的事实的结果)。

那加文·辛普森提到的解决方法:

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



Answer 2:

你可以使用这一招,

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



Answer 3:

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)

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


2)用溶液ggplot2::annotation_custom()

如所提到的, cowplot::draw_label()是一个包装ggplot2::annotation_custom() 所以,与其cowplot::draw_label()我们可以直接使用ggplot2::annotation_custom()结合设置剪下- coord_cartesian(clip = "off")这成为可用的合并此pull请求 。

然而,这种方法更详细的,更协调的参数,我们需要采用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)

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



文章来源: ggplot2 two-line label with expression
标签: r ggplot2