R 中的 ggplotly:更改数据标签大小

ggplotly in R : change data label size

我必须将 ggplot 转换为 plotly。当然,最简单的方法似乎是使用 ggploty,但字体大小会发生变化。恢复轴标签和标题的合适大小没问题,但我不知道如何为数据标签做这个?有任何想法吗?我确实更喜欢保留 ggplot 并用 ggplotly 包裹它(还有许多其他地块)。

我尝试了布局中的字体但没有成功:

library(ggplot2)
library(plotly)

tbl <- data.frame(
  indicateur = c("freq1", "freq2", "freq3" ),
  valeur = c(44, 78, 84)
)

ggplotly(
  ggplot(tbl) +
    geom_bar(aes(x = indicateur, y = valeur), stat = 'identity', fill = rgb(31, 119, 180, maxColorValue = 255)) +
    geom_text(aes(x = indicateur, y = valeur, label = valeur), vjust = -0.5) + 
    ylim(0, max(tbl$valeur)*1.1) +
    labs(x = "", y = "", title = "simple counting") +
    theme_bw() +
    theme(
      axis.line = element_blank(), 
      panel.border = element_blank(),
      plot.title = element_text(hjust = 0.5)
    )
  ) %>%
  layout(
    showlegend = FALSE, 
    textfont = list(size = 5), 
    titlefont = list(size = 12),
    xaxis = list(tickfont = list(size = 9))
  )

在此先感谢您的帮助。

size 传递给 geom_text 怎么样?此外,我不认为所有的布局调整都是必要的。请参阅以下内容:

library(tidyverse)
library(plotly)

tbl <- data.frame(
  indicateur = c("freq1", "freq2", "freq3" ),
  valeur = c(44, 78, 84)
)

p_size_5 <- ggplot(tbl, aes(x = indicateur, y = valeur, label = valeur)) +
  geom_bar(stat = "identity", fill = rgb(31, 119, 180, maxColorValue = 255)) +
  geom_text(vjust = -0.5, size = 5) +
  labs(
    title = "simple counting (text 5)",
    x = NULL, y = NULL
  ) +
  theme_bw() +
  theme(
    axis.line = element_blank(),
    panel.border = element_blank(),
    plot.title = element_text(hjust = 0.5)
  )

ggplotly(p_size_5)

p_size_2 <- ggplot(tbl, aes(x = indicateur, y = valeur, label = valeur)) +
  geom_bar(stat = "identity", fill = rgb(31, 119, 180, maxColorValue = 255)) +
  geom_text(vjust = -0.5, size = 2) +
  labs(
    title = "simple counting (text 2)",
    x = NULL, y = NULL
  ) +
  theme_bw() +
  theme(
    axis.line = element_blank(),
    panel.border = element_blank(),
    plot.title = element_text(hjust = 0.5)
  )

ggplotly(p_size_2)