Home > Net >  Adjusting the format of percentage in ggplotly hoover info in R
Adjusting the format of percentage in ggplotly hoover info in R

Time:10-08

please help:

How to format percentage in the hoover info on a ggplotly graph? I have this format now: enter image description here

However, I want it to have instead 0.10611384 this format: 10% (without decimals).

I tried with this code:

 ggplot(data=ESIF_zajmovi_sektori, aes(x=Mjera, y=Udio, fill=Sektor))  geom_bar(stat="identity", position="dodge", text = paste0("Mjera: ", ESIF_zajmovi_sektori$Mjera,
                                                       '<br>',
                                                       "Sektor: ", ESIF_zajmovi_sektori$Sektor, 
                                                       '<br>',
                                                       "% Udio:", scales::percent(ESIF_zajmovi_sektori$Udio))) scale_fill_viridis(discrete = TRUE, option = "plasma")  labs(x= "ESIF mjera", y="% udio u ukupnom iznosu") scale_y_continuous(labels = scales::percent) theme_minimal() `ggplotly(G3, tooltip = "text")` 

As you see I am using the text function in geom_bar() but when I do this I get the graph without any hoover-info! Please help, Tnx

CodePudding user response:

To achieve your desired result put the text aesthetic inside aes().

library(plotly)
library(viridis)

ESIF_zajmovi_sektori <- data.frame(
  Mjera = 1,
  Udio = 1:4,
  Sektor = letters[1:4]
)

G3 <- ggplot(data = ESIF_zajmovi_sektori, aes(x = Mjera, y = Udio, fill = Sektor))  
  geom_bar(stat = "identity", position = "dodge", aes(text = paste0(
    "Mjera: ", Mjera,
    "<br>",
    "Sektor: ", Sektor,
    "<br>",
    "% Udio:", scales::percent(Udio)
  )))  
  scale_fill_viridis(discrete = TRUE, option = "plasma")  
  labs(x = "ESIF mjera", y = "% udio u ukupnom iznosu")  
  scale_y_continuous(labels = scales::percent)  
  theme_minimal()

ggplotly(tooltip = "text")

enter image description here

  • Related