如何设置文本以在工具提示上显示货币(使用 plot_ly NOT ggplotly)?
How to set text to show currency on tooltip (using plot_ly NOT ggplotly)?
据我所知,在生成 plotly 对象时需要设置 "text" 参数,然后将此 "text" 参数称为工具提示。
数据:
ha <- structure(list(periodo = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2017",
"2016"), class = c("ordered", "factor")), ecommerce = structure(c(2L,
2L, 2L, 2L, 2L, 2L), .Label = c("falabella", "ripley", "linio"
), class = c("ordered", "factor")), marca = c("samsung", "samsung",
"lg", "lg", "samsung", "lg"), producto = c("samsung tv led hd 32'' 32j4000",
"samsung smart tv led fhd 48\"\" 3d 48j6400", "lg smart tv led 43'' full hd 43lh5700",
"lg smart tv led 49'' full hd 49lh5700", "samsung smart tv 50ju6500 led uhd 50\"\" - negro",
"lg smart tv led 49\"\" ultra hd tv 49uh6500"), precio.antes = c(999,
2799, 1649, 1999, 3699, 2799), precio.actual = c(799, 1999, 1249,
1699, 2399, 2199), pulgadas = c(32, 48, 43, 49, 50, 49), rango = c("S/.500 -\r\n S/.1500",
"S/.1500 -\r\n S/.2500", "S/.500 -\r\n S/.1500", "S/.1500 -\r\n S/.2500",
"S/.1500 -\r\n S/.2500", "S/.1500 -\r\n S/.2500"), descuento = c(-0.2002002002002,
-0.285816362986781, -0.242571255306246, -0.150075037518759, -0.351446336847797,
-0.214362272240086)), row.names = c(NA, 6L), class = "data.frame")
在 ggplotly 中,文本参数被放置在 aes()
函数中。
当使用 plot_ly 时,我把它放在里面:
p <- plot_ly(ha, x = ~periodo, y = ~precio.actual, color = ~ecommerce,colors = c("#BED800", "#802D69", "#FF5500"), text = sprintf("S/ %s", comma(~precio.actual)), textinfo= "text", hoverinfo = "text") %>%
add_boxplot() %>%
layout(boxmode = "group") %>%
config(displayModeBar = FALSE)
p
上面的代码对工具提示的格式没有影响。
显示工具提示但没有所需的货币格式。
不是2399,应该是S/2,399,例如
您可以通过传递带有 hoverformat
的列表来格式化 layout
中的 y 变量,它采用格式字符串,如 sprintf
.
plot_ly(ha, x = ~periodo, y = ~precio.actual, color = ~ecommerce,
colors = c("#BED800", "#802D69", "#FF5500")) %>%
add_boxplot() %>%
layout(yaxis = list(
hoverformat = '$,.2f'
)) %>%
config(displayModeBar = FALSE)
如果你想更高级一点,可以使用hovertemplate
。这里有很多例子:https://plot.ly/r/hover-text-and-formatting/
据我所知,在生成 plotly 对象时需要设置 "text" 参数,然后将此 "text" 参数称为工具提示。
数据:
ha <- structure(list(periodo = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2017",
"2016"), class = c("ordered", "factor")), ecommerce = structure(c(2L,
2L, 2L, 2L, 2L, 2L), .Label = c("falabella", "ripley", "linio"
), class = c("ordered", "factor")), marca = c("samsung", "samsung",
"lg", "lg", "samsung", "lg"), producto = c("samsung tv led hd 32'' 32j4000",
"samsung smart tv led fhd 48\"\" 3d 48j6400", "lg smart tv led 43'' full hd 43lh5700",
"lg smart tv led 49'' full hd 49lh5700", "samsung smart tv 50ju6500 led uhd 50\"\" - negro",
"lg smart tv led 49\"\" ultra hd tv 49uh6500"), precio.antes = c(999,
2799, 1649, 1999, 3699, 2799), precio.actual = c(799, 1999, 1249,
1699, 2399, 2199), pulgadas = c(32, 48, 43, 49, 50, 49), rango = c("S/.500 -\r\n S/.1500",
"S/.1500 -\r\n S/.2500", "S/.500 -\r\n S/.1500", "S/.1500 -\r\n S/.2500",
"S/.1500 -\r\n S/.2500", "S/.1500 -\r\n S/.2500"), descuento = c(-0.2002002002002,
-0.285816362986781, -0.242571255306246, -0.150075037518759, -0.351446336847797,
-0.214362272240086)), row.names = c(NA, 6L), class = "data.frame")
在 ggplotly 中,文本参数被放置在 aes()
函数中。
当使用 plot_ly 时,我把它放在里面:
p <- plot_ly(ha, x = ~periodo, y = ~precio.actual, color = ~ecommerce,colors = c("#BED800", "#802D69", "#FF5500"), text = sprintf("S/ %s", comma(~precio.actual)), textinfo= "text", hoverinfo = "text") %>%
add_boxplot() %>%
layout(boxmode = "group") %>%
config(displayModeBar = FALSE)
p
上面的代码对工具提示的格式没有影响。
显示工具提示但没有所需的货币格式。 不是2399,应该是S/2,399,例如
您可以通过传递带有 hoverformat
的列表来格式化 layout
中的 y 变量,它采用格式字符串,如 sprintf
.
plot_ly(ha, x = ~periodo, y = ~precio.actual, color = ~ecommerce,
colors = c("#BED800", "#802D69", "#FF5500")) %>%
add_boxplot() %>%
layout(yaxis = list(
hoverformat = '$,.2f'
)) %>%
config(displayModeBar = FALSE)
如果你想更高级一点,可以使用hovertemplate
。这里有很多例子:https://plot.ly/r/hover-text-and-formatting/