繁体   English   中英

如何访问由 `ggplot2` 中的 `geom_text` 绘制的标签尺寸?

[英]How can I access dimensions of labels plotted by `geom_text` in `ggplot2`?

据我所知ggplot2知道geom_text绘制的标签的尺寸。 否则check_overlap选项将不起作用。

这些维度存储在哪里以及如何访问它们?


示例

library(ggplot2)
df <- data.frame(x = c(1, 2), 
                 y = c(1, 1), 
                 label = c("label-one-that-might-overlap-another-label", 
                           "label-two-that-might-overlap-another-label"), 
                 stringsAsFactors = FALSE)

使用check_overlap = FALSE (默认值),标签相互check_overlap = FALSE

ggplot(df, aes(x, y)) + 
  geom_text(aes(label = label)) + 
  xlim(0, 3)                              

在此处输入图片说明

使用check_overlap = TRUE ,不会绘制第二个标签,因为ggplot发现了重叠。

ggplot(df, aes(x, y)) + 
  geom_text(aes(label = label), check_overlap = TRUE) + 
  xlim(0, 3)

在此处输入图片说明

ggplot2如何知道这些标签重叠? 我怎样才能访问这些信息?

在绘制文本之前,grid 包中的文本并没有真正的大小。 下面,我们将创建一个辅助函数来测量文本,但除非您事先知道绘图区域的设备和大小,否则这样做没有任何意义。 (对于那些makeContent() ,在绘制的makeContent()阶段)。

library(grid)

label <- c("label-one-that-might-overlap-another-label", 
           "label-two-that-might-overlap-another-label")

measure_size <- function(txt, gp = gpar(), to = "mm") {
  if (is.grob(txt)) {
    grobs <- lapply(seq_along(txt$label), function(i) {
      g <- txt
      # Subset grob per label
      g$label <- g$label[[i]]
      g$gp[]  <- lapply(g$gp, function(x) {x[pmin(i, length(x))]})
      g$rot   <- g$rot[pmin(i, length(g$rot))]
      g
    })
  } else {
    grobs <- lapply(txt, function(t) textGrob(t, gp = gp))
  }
  
  heights <- do.call(unit.c, lapply(grobs, grobHeight))
  widths  <- do.call(unit.c, lapply(grobs, grobWidth))
  
  cbind(
    height = convertHeight(heights, to, valueOnly = TRUE),
    weight = convertWidth(widths,   to, valueOnly = TRUE)
  )
}

我们现在可以尽可能地猜测文本的大小,但正如人们所料,它在很大程度上取决于文本的图形参数,实际大小是多少。 请注意,例如更改字体也会更改文本的大小。

measure_size(label)
#>      height   weight
#> [1,]  3.175 79.13109
#> [2,]  3.175 78.65566

measure_size(label, gp = gpar(fontfamily = "Garamond"))
#>        height   weight
#> [1,] 2.645833 69.67223
#> [2,] 2.645833 69.69704

现在将相同的技巧应用于 ggplot2 的文本层。

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.1.1

df <- data.frame(x = c(1, 2), 
                 y = c(1, 1), 
                 label = label)

p <- ggplot(df, aes(x, y)) + 
  geom_text(aes(label = label)) + 
  xlim(0, 3) 

textgrob <- layer_grob(p)[[1]]
measure_size(textgrob)
#>        height   weight
#> [1,] 2.645979 72.83233
#> [2,] 2.645979 72.39411

reprex 包(v2.0.1) 于 2021 年 12 月 13 日创建

我最近自己处理了很多文本,发现 {systemfonts}/{textshaping} 包准确地以像素为单位返回文本的大小,这当然取决于设备/分辨率。

systemfonts::string_width("My label")
#> [1] 46
textshaping::text_width("My label")
#> [1] 46

据我所知ggplot2知道geom_text绘制的标签的尺寸。 否则, check_overlap选项将不起作用。

这些维度存储在哪里,如何访问它们?


说明性的例子

library(ggplot2)
df <- data.frame(x = c(1, 2), 
                 y = c(1, 1), 
                 label = c("label-one-that-might-overlap-another-label", 
                           "label-two-that-might-overlap-another-label"), 
                 stringsAsFactors = FALSE)

如果check_overlap = FALSE (默认值),则标签会相互check_overlap = FALSE

ggplot(df, aes(x, y)) + 
  geom_text(aes(label = label)) + 
  xlim(0, 3)                              

在此处输入图片说明

如果check_overlap = TRUE ,则不会绘制第二个标签,因为ggplot发现重叠。

ggplot(df, aes(x, y)) + 
  geom_text(aes(label = label), check_overlap = TRUE) + 
  xlim(0, 3)

在此处输入图片说明

ggplot2如何知道这些标签重叠? 我如何访问该信息?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM