Text layer works in ggplot but removed with ggplotly

I have a barplot with some text layer, everything works fine with the ggplot library, but now I want to add some interactivity with ggplotly, but does not display the text layer I updated all the packages, but the problem persists.

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)

Here is what I get with ggplot2 and plotly:

enter image description here

Tight (can I remove the plotly toolbar?) enter image description here

My ggplot graphics are fine, but not working with ggplotly, can we solve it? My second question is: is it possible to delete the toolbar according to the plot (because we can only customize the graphics with the mouse)?

Thank you Thank you for your help.

+4
source share
1 answer

You could do

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

enter image description here

The problem looks like an error. Perhaps you want to record it on Github.

+2
source

Source: https://habr.com/ru/post/1666777/


All Articles