【问题标题】:Text layer work in ggplot but removed with ggplotly文本层在 ggplot 中工作,但被 ggplotly 删除
【发布时间】:2017-05-29 18:08:20
【问题描述】:

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

df = read.table(text = "
id   year  type amount
                1  1991  HIIT     22
                2  1991 inter    144
                3  1991  VIIT     98
                4  1992  HIIT     20
                5  1992 inter    136
                6  1992  VIIT    108
                7  1993  HIIT     20
                8  1993 inter    120
                9  1993  VIIT    124
                10 1994  HIIT     26
                11 1994 inter    118
                12 1994  VIIT    120
                13 1995  HIIT     23
                14 1995 inter    101
                15 1995  VIIT    140
                16 1996  HIIT     27
                17 1996 inter    103
                18 1996  VIIT    162
                19 1997  HIIT     24
                20 1997 inter     96
                21 1997  VIIT    172
                22 1998  HIIT     24
                23 1998 inter     92
                24 1998  VIIT    177
                25 1999  HIIT     28
                26 1999 inter     45
                27 1999  VIIT    220
                28 2000  HIIT     26
                29 2000 inter     36
                30 2000  VIIT    231", header = TRUE, sep = "")

df %>%
  group_by(year) %>%
  mutate(type = factor(type, levels = c("inter",  "VIIT", "HIIT"))) %>%
  mutate(ratio = amount / sum(amount)) %>%
  ggplot(aes(x = factor(year), y = ratio, fill = type)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = percent(ratio)), position = position_stack(vjust = 0.5), size = 4) +
  scale_y_continuous(name="", labels = percent) +
  coord_flip() +
  xlab("Pourcentage") +
  ggtitle("Répartition des types par année") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme_minimal()-> gg

ggplotly(gg)

这是我用 ggplot2 和 plotly 得到的:

绘图(是否可以删除绘图工具栏?)

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

谢谢 感谢您的帮助

【问题讨论】:

  • 如果您有交互式图表,为什么还要使用文本标签?例如,如果您排除图例中的类型,则标签不会调整。

标签: r ggplot2 plotly


【解决方案1】:

你可以的

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

这个问题看起来像一个错误。也许你想在 Github 上归档。

【讨论】:

  • 删除工具栏也可以使用 ggplotly(gg) %>% config(displayModeBar = F)
  • 非常感谢@LukeA 正是我正在寻找的东西:D @MLavoie 也感谢您的精确
猜你喜欢
  • 2016-04-09
  • 2021-12-16
  • 1970-01-01
  • 2020-03-27
  • 1970-01-01
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多