gpt4 book ai didi

r - ggplot2:如何动态换行/调整大小/重新缩放 x 轴标签,使它们不会重叠

转载 作者:行者123 更新时间:2023-12-05 03:45:13 29 4
gpt4 key购买 nike

我正在尝试实现一种包装 x 轴标签的解决方案,这样它们就不会重叠。我知道这个问题已经被问过好几次了,并且有 some good answers .但是,我所见过的解决方案都没有回答如何在绘图调整大小时重新包装标签。

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

  1. This solution编写了一个定制的 geom 来调整 bar 的标签 size 以适应 bar 的宽度,当你调整绘图大小时动态。

  2. This solution ggplot2 依赖于名为 ggtext扩展 包.该解决方案允许在您根据创建 element_textbox() 调整绘图大小时,对绘图的标题 进行动态换行。 .

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

可使用的一些演示数据

<强>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 package 创建于 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 package 创建于 2021-01-29 (v0.3.0)


<强>3。 ggfittext 具有促进文本换行的 reflow 参数

  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 package 创建于 2021-01-29 (v0.3.0)


我的问题

我不知道该怎么做,但是我们可以通过某种方式让 ggfittext 为我们完成艰苦的工作,从而动态地包裹/调整大小/重新缩放 x 轴标签吗?以我幼稚的方式来看,条形内的文本已经以正确的方式呈现,我们能否以某种方式将此呈现“复制”到轴标签?

最佳答案

我们将 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 package 创建于 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 package 创建于 2021-01-29 (v0.3.0)

关于r - ggplot2:如何动态换行/调整大小/重新缩放 x 轴标签,使它们不会重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65956833/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com