1

I am trying to make a plotly and show when you hover over the boxplot the values but in $ format. The plot is faceted by group and I get the correct results in the first facet but not in the other two. How can I make it with all the facets? The code that I have tried so far is as follows:

#Package used
library(tidyverse)
library(plotly)
#Structure of the data for reprex and code, no seed needed
#First 50 observation of the df
n1 <- structure(list(.value = c(8309084934, 8309084934, 8309084934, 
6978206450, 6978206450, 6978206450, 8149976767, 8149976767, 8149976767, 
7282783517, 7282783517, 7282783517, 7442214542, 7442214542, 7442214542, 
7578040086, 7578040086, 7578040086, 9203243548, 9203243548, 9203243548, 
7937908214, 7937908214, 7937908214, 7850863043, 7850863043, 7850863043, 
7419201212, 7419201212, 7419201212, 7709377441, 7709377441, 7709377441, 
8584235120, 8584235120, 8584235120, 11207703304, 11207703304, 
11207703304, 8348916694, 8348916694, 8348916694, 8811891601, 
8811891601, 8811891601, 8366340368, 8366340368, 8366340368, 8169901991, 
8169901991), .group = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L), .Label = c("month.lbl", 
"quarter", "year"), class = "factor"), .group_value = structure(c(1L, 
13L, 17L, 2L, 13L, 17L, 3L, 13L, 17L, 4L, 14L, 17L, 5L, 14L, 
17L, 6L, 14L, 17L, 7L, 15L, 17L, 8L, 15L, 17L, 9L, 15L, 17L, 
10L, 16L, 17L, 11L, 16L, 17L, 12L, 16L, 17L, 1L, 13L, 18L, 2L, 
13L, 18L, 3L, 13L, 18L, 4L, 14L, 18L, 5L, 14L), .Label = c("enero", 
"febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", 
"septiembre", "octubre", "noviembre", "diciembre", "1", "2", 
"3", "4", "2000", "2001", "2002", "2003", "2004", "2005", "2006", 
"2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", 
"2015", "2016", "2017", "2018", "2019", "2020", "2021"), class = "factor")), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame")) %>% 
  ggplot(aes(x = .group_value, y = .value)) +
  geom_boxplot() +
  facet_wrap(~.group, scales = "free") +
  coord_flip() +
  scale_y_continuous(labels = scales::number_format(scale = 1e-9, suffix = "M.M")) +
  labs(
    y = "M.M de pesos",
    title = "Diagnostico de la serie de aportes",
    caption = "CCA"
  ) +
  theme(plot.title = element_text(hjust = 0.5))

ggplotly(n1) %>% 
  layout(xaxis = list(
        hoverformat = '$,.1f'
    ))

That is my first attempt and the closest one. I tried also to put inside the geom_boxplot() the parameter text = paste(...) but it doesent work. I guess it is because when you hover over the boxplot you get min, max, median... and not a plain text. Here is an image of what I get now :

enter image description here

So if you re-run the code, you can see that we only get the $ format in the first facet, but the point is to get the $ format in all the facets. Thanks a lot!

  • It looks like you're fairly new to SO; welcome to the community! If you want great answers quickly, it's best to make your question reproducible. This includes sample data like the output from `dput(head(dataObject)))`. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). – Kat Jan 31 '22 at 22:47

1 Answers1

0

I took what you had designated for xaxis and made it an object:

frmtr = list(hoverformat = '$,.1f')

Then I used that object to simplify the application of this formatting to each x-axis

ggplotly(n1) %>% 
  layout(xaxis = frmtr,
         xaxis2 = frmtr,
         xaxis3 = frmtr)

enter image description here

enter image description here

Kat
  • 15,669
  • 3
  • 18
  • 51