文本层在 ggplot 中工作,但用 ggplotly 删除

Mos*_*afa 6 r ggplot2 plotly

我有一个带有一些文本层的条形图,所有这些都与 ggplot 库一起正常工作,但现在我想添加一些与 ggplotly 的交互性,但它无法显示文本层\n我更新了所有包,但问题仍然存在

\n
df = read.table(text = "\nid   year  type amount\n                1  1991  HIIT     22\n                2  1991 inter    144\n                3  1991  VIIT     98\n                4  1992  HIIT     20\n                5  1992 inter    136\n                6  1992  VIIT    108\n                7  1993  HIIT     20\n                8  1993 inter    120\n                9  1993  VIIT    124\n                10 1994  HIIT     26\n                11 1994 inter    118\n                12 1994  VIIT    120\n                13 1995  HIIT     23\n                14 1995 inter    101\n                15 1995  VIIT    140\n                16 1996  HIIT     27\n                17 1996 inter    103\n                18 1996  VIIT    162\n                19 1997  HIIT     24\n                20 1997 inter     96\n                21 1997  VIIT    172\n                22 1998  HIIT     24\n                23 1998 inter     92\n                24 1998  VIIT    177\n                25 1999  HIIT     28\n                26 1999 inter     45\n                27 1999  VIIT    220\n                28 2000  HIIT     26\n                29 2000 inter     36\n                30 2000  VIIT    231", header = TRUE, sep = "")\n\ndf %>%\n  group_by(year) %>%\n  mutate(type = factor(type, levels = c("inter",  "VIIT", "HIIT"))) %>%\n  mutate(ratio = amount / sum(amount)) %>%\n  ggplot(aes(x = factor(year), y = ratio, fill = type)) +\n  geom_bar(stat = "identity") +\n  geom_text(aes(label = percent(ratio)), position = position_stack(vjust = 0.5), size = 4) +\n  scale_y_continuous(name="", labels = percent) +\n  coord_flip() +\n  xlab("Pourcentage") +\n  ggtitle("R\xc3\xa9partition des types par ann\xc3\xa9e") +\n  theme(plot.title = element_text(hjust = 0.5)) +\n  theme_minimal()-> gg\n\nggplotly(gg)\n
Run Code Online (Sandbox Code Playgroud)\n

这是我使用 ggplot2 和plotly 得到的结果:

\n

在此输入图像描述

\n

Plotly(是否可以删除 Plotly 工具栏?)\n在此输入图像描述

\n

我在 ggplot 上的图形没问题,但不能与 ggplotly 一起使用,我们可以解决它吗?\n我的第二个问题,是否可以删除绘图上的工具栏(因为我们只能用鼠标调整图形)?

\n

谢谢\n感谢您的帮助

\n

luk*_*keA 4

你可以做

gp <- ggplotly(gg)
for (x in 4:6)
  gp$x$data[[x]]$x <- 1-gp$x$data[[x]]$x 
gp$x$config$displayModeBar <- FALSE
gp
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这个问题看起来像一个错误。也许您想将其归档到 Github 上。

  • 要删除工具栏,您还可以使用 ggplotly(gg) %&gt;% config(displayModeBar = F) (2认同)