1

我正在尝试实现一种包装 x 轴标签的解决方案,这样它们就不会重叠。我知道这个问题已经被问过好几次了,并且有一些很好的答案。但是,我没有看到任何解决方案可以回答如何在情节调整大小时重新包装标签。

SO 的三个不同答案让我相信这是可以实现的。

  1. 该解决方案编写了一个定制的工具,geom用于在调整绘图大小时动态地调整 bar的标签以适应 bar 的宽度size

  2. 此解决方案依赖于被调用的扩展包。该解决方案允许在根据创建.ggplot2ggtextelement_textbox()

  3. 此解决方案依赖于另一个名为ggfittext. 它显示了size当您调整绘图大小时,条形内的标签如何动态变化以适应条形的尺寸。本质上,它解决了与上述解决方案 (1) 相同的问题,但功能更强大。事实上,这是让我充满希望的特性,它依赖于一个通用的解决方案geom_fit_text()来适应矩形内的文本,而不仅仅是geom_bar()s。

一些可使用的演示数据

1. 仅显示 x 轴标签重叠时的典型输出

  library(tidyverse)
  
  my_mtcars <-
    mtcars[15:20,] %>% 
    rownames_to_column("cars")
  
  my_mtcars %>%
    ggplot(aes(x = cars, y = mpg, fill = cars)) + 
    geom_bar(stat = "identity")

reprex 包于 2021-01-29 创建(v0.3.0)


2.当我们使用时,ggfittext我们可以看到条形内的标签如何缩小以适应条形

  library(tidyverse)
  library(ggfittext)
#> Warning: package 'ggfittext' was built under R version 4.0.3
  
  my_mtcars <-
    mtcars[15:20,] %>% 
    rownames_to_column("cars")
  
  my_mtcars %>%
    ggplot(aes(x = cars, y = mpg, fill = cars)) + 
    geom_bar(stat = "identity") +
    geom_bar_text(aes(label = cars), 
      color = "blue", 
      vjust = 1, 
      size = 7 * ggplot2::.pt, 
      min.size = 0,
      padding.x = grid::unit(0, "pt"),
      padding.y = grid::unit(0, "pt"))
#> Warning: Ignoring unknown aesthetics: label

reprex 包于 2021-01-29 创建(v0.3.0)


3.ggfittextreflow提倡文字换行的论据

  library(tidyverse)
  library(ggfittext)
#> Warning: package 'ggfittext' was built under R version 4.0.3
  
  my_mtcars <-
    mtcars[15:20,] %>% 
    rownames_to_column("cars")
  
  my_mtcars %>%
    ggplot(aes(x = cars, y = mpg, fill = cars)) + 
    geom_bar(stat = "identity") +
    geom_bar_text(aes(label = cars), 
      color = "blue", 
      vjust = 1, 
      size = 7 * ggplot2::.pt, 
      min.size = 0,
      padding.x = grid::unit(0, "pt"),
      padding.y = grid::unit(0, "pt"),
      reflow = TRUE ## <--------------- added this
      )
#> Warning: Ignoring unknown aesthetics: label

reprex 包于 2021-01-29 创建(v0.3.0)


我的问题

我不知道该怎么做,但是我们可以通过某种方式让ggfittext我们为我们做艰苦的工作,来动态地包装/调整大小/重新调整 x 轴标签吗?在我看到的天真的方式中,条形图中的文本已经以正确的方式呈现,我们可以以某种方式将这个呈现“复制”到轴标签吗?

4

2 回答 2

1

我们将ggfittext文本放在 y 轴下方怎么样?我们关闭裁剪并设置ooblimits以适合我们的数据。可能应该调整axis.text.x大小以更好地与 x 轴标题对齐。

library(tidyverse)
#> Warning: package 'tidyr' was built under R version 4.0.3
#> Warning: package 'readr' was built under R version 4.0.3
#> Warning: package 'dplyr' was built under R version 4.0.3
library(ggfittext)
#> Warning: package 'ggfittext' was built under R version 4.0.3

my_mtcars <-
  mtcars[15:20,] %>% 
  rownames_to_column("cars")

my_mtcars %>%
  ggplot(aes(x = cars, y = mpg, fill = cars)) + 
  geom_bar(stat = "identity") +
  geom_fit_text(aes(label = cars, y = -4),
                reflow = TRUE, height = 50,
                show.legend = FALSE) +
  scale_y_continuous(oob = scales::oob_keep,
                     limits = c(0, NA)) +
  coord_cartesian(clip = "off") +
  theme(axis.text.x = element_text(colour = "transparent", size = 18))

reprex 包于 2021-01-29 创建(v0.3.0)

编辑:从 grob 中取出标签

library(tidyverse)
library(ggfittext)

my_mtcars <-
  mtcars[15:20,] %>% 
  rownames_to_column("cars")

p <- my_mtcars %>%
  ggplot(aes(x = cars, y = mpg, fill = cars)) + 
  geom_bar(stat = "identity") +
  geom_fit_text(aes(label = cars, y = -1),
                reflow = TRUE, height = 50,
                show.legend = FALSE) +
  scale_y_continuous(oob = scales::oob_keep,
                     limits = c(0, NA)) +
  coord_cartesian(clip = "off") +
  theme(axis.text.x = element_text(colour = "transparent", size = 18))

grob <- grid::makeContent(layer_grob(p, 2)[[1]])$children

sizes <- vapply(grob, function(x){x$gp$fontsize}, numeric(1))
labels <- unname(vapply(grob, function(x){x$label}, character(1)))
print(labels)
#> [1] "Cadillac\nFleetwood"  "Lincoln\nContinental" "Chrysler\nImperial"  
#> [4] "Fiat 128"             "Honda Civic"          "Toyota\nCorolla"

reprex 包于 2021-01-29 创建(v0.3.0)

于 2021-01-29T17:01:47.883 回答
0

只是改变文本的角度或大小怎么样?

角度:

my_mtcars %>%
  ggplot(aes(x = cars, y = mpg, fill = cars)) + 
  geom_bar(stat = "identity")+
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))

尺寸:

my_mtcars %>%
  ggplot(aes(x = cars, y = mpg, fill = cars)) + 
  geom_bar(stat = "identity")+
  theme(axis.text.x = element_text(size = 4)) 
于 2021-01-29T15:26:53.487 回答