如何使所有悬停格式以 plotly 显示“$”中的数字?
How to make all the hover formats show the numbers in "$" with plotly?
我正在尝试制作一个绘图,并在您将鼠标悬停在箱线图上时显示值,但采用 $ 格式。该图按组分面,我在第一个方面得到正确的结果,但在其他两个方面没有。我怎样才能做到所有方面?到目前为止我尝试过的代码如下:
#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'
))
这是我的第一次尝试,也是最接近的一次。我也尝试将参数 text = paste(...)
放入 geom_boxplot()
中,但它不起作用。我想这是因为当您将鼠标悬停在箱线图上时,您会得到 min
、max
、median
... 而不是纯文本。这是我现在得到的图片:
所以如果你重新运行代码,你可以看到我们只在第一个切面中获取$格式,但重点是在所有切面中获取$格式。非常感谢!
我把你为xaxis
指定的东西做了一个对象:
frmtr = list(hoverformat = '$,.1f')
然后我使用该对象来简化此格式在每个 x-axis
中的应用
ggplotly(n1) %>%
layout(xaxis = frmtr,
xaxis2 = frmtr,
xaxis3 = frmtr)
我正在尝试制作一个绘图,并在您将鼠标悬停在箱线图上时显示值,但采用 $ 格式。该图按组分面,我在第一个方面得到正确的结果,但在其他两个方面没有。我怎样才能做到所有方面?到目前为止我尝试过的代码如下:
#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'
))
这是我的第一次尝试,也是最接近的一次。我也尝试将参数 text = paste(...)
放入 geom_boxplot()
中,但它不起作用。我想这是因为当您将鼠标悬停在箱线图上时,您会得到 min
、max
、median
... 而不是纯文本。这是我现在得到的图片:
所以如果你重新运行代码,你可以看到我们只在第一个切面中获取$格式,但重点是在所有切面中获取$格式。非常感谢!
我把你为xaxis
指定的东西做了一个对象:
frmtr = list(hoverformat = '$,.1f')
然后我使用该对象来简化此格式在每个 x-axis
中的应用ggplotly(n1) %>%
layout(xaxis = frmtr,
xaxis2 = frmtr,
xaxis3 = frmtr)