ggiraph 字体大小 HTML

问题描述 投票:0回答:1

我在 RStudio 中使用 ggiraph 和 geom_tile_interactive。该图表在查看器窗口中呈现精美,但在转换为 HTML 并将鼠标悬停在在线图表上后,字体太小而无法阅读。我正在使用 ggiraph 0.8.7 和最新的 CRAN 版本的 htmlwidgets。这是我的代码

g <- ggplot(gg.dat, aes(x = class, y = var.lab, fill = probability,
                        tooltip = paste0(class, "\n", var.lab, "\n", probability),
                                       data_id = var.lab)) +
         geom_tile_interactive(width = .75)

    g.int <- girafe(ggobj = g)

    saveWidget(g.int, file = paste0(my.dir, "/", my.graph.name, ".html), 
               selfcontained = TRUE)

如有任何帮助,我们将不胜感激。

r ggplot2 font-size ggiraph
1个回答
0
投票

虽然您可以使用

theme()
size=
设置图表本身的字体大小,如工具提示的
ggplot2
所示,但您必须使用
options=
girafe()
参数设置字体大小。为此,
ggiraph
提供了一个辅助函数
opts_tooltip()
,它允许通过
css=
参数设置工具提示的样式,例如在下面的代码中,我将
font-size
设置为
4rem

使用基于 mtcars 的最小可重现示例:

library(ggiraph)
library(ggplot2)
library(dplyr, warn = FALSE)

gg.dat <- mtcars |>
  group_by(class = factor(cyl), var.lab = factor(am)) |>
  summarise(
    probability = mean(mpg),
    .groups = "drop"
  )

g <- ggplot(gg.dat, aes(
  x = class, y = var.lab, fill = probability,
  tooltip = paste0(class, "\n", var.lab, "\n", probability),
  data_id = var.lab
)) +
  geom_tile_interactive(width = .75) +
  theme(text = element_text(size = 20))

g.int <- girafe(
  ggobj = g,
  options = list(
    opts_tooltip(
      use_fill = TRUE,
      css = "padding:5pt;font-size:4rem;color:white"
    )
  )
)

htmlwidgets::saveWidget(g.int,
  file = "foo.html",
  selfcontained = TRUE
)

© www.soinside.com 2019 - 2024. All rights reserved.