R:使用ggplotly或plotly在工具提示上设置货币格式

奥马尔·冈萨雷斯

我正在制作图表,首先在ggplot中进行绘制,然后使其与ggplotly进行交互。

但我需要正确格式化价格的工具提示(在data.frame中称为“ precio_actual”)。

在工具提示中显示为:1499。

应为:S / 1,4900.00。

我正在阅读ggplotly文档,但找不到有关如何实现此目的的任何指示。

资料

dput(tail_tvs)
structure(list(ecommerce = c("wong", "wong", "wong", "wong", 
"wong", "wong"), marca = c("sony", "samsung", "sony", "samsung", 
"daewoo", "samsung"), producto = c("sony smart tv 55'' 4k uhd kd-55x750f android", 
"samsung smart tv curvo 65'' 4k uhd 65nu7300", "sony smart tv 40'' full hd kdl-40w655d linux", 
"samsung smart tv 55'' 4k uhd 55mu6103", "daewoo smart tv 43'' full hd l43s780bts", 
"samsung smart tv 49'' 4k uhd 49mu6400"), precio_antes = c(4499, 
4999, 1699, 3599, 1439, 3999), precio_actual = c(2199, 4999, 
1299, 3599, 1439, 3999), pulgadas = c(55, 65, 40, 55, 43, 49), 
    unidades = c(2, 1, 4, 1, 1, 2), descuento = c(-0.51122471660369, 
    0, -0.235432607416127, 0, 0, 0), rango = c("S/.1500 - S/.2500", 
    "> S/.4,500", "S/.500 - S/.1500", "S/.3500 - S/.4500", "S/.500 - S/.1500", 
    "S/.3500 - S/.4500")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L))

码:

tt10 <- "Precio de televisores según su tamaño (pulgadas)"
stt10 <- "\n"


pulgadas_precio <- ggplot(tail_tvs, aes(x = pulgadas, y = precio_actual)) + 
                    geom_point(aes(color=marca),size = 4,alpha = 0.6) +
                    facet_grid(~ ecommerce) +
                    theme_ipsum_rc(grid = "Y") +
                    theme(axis.text.x = element_text(colour="grey10",size=10,hjust=.5,vjust=.5,face="plain"),
                          axis.text.y = element_text(colour="grey10",size=10,hjust=1,vjust=0,face="plain"),  
                          axis.title.x = element_text(colour="grey40",size=16,angle=0,hjust=.5,vjust=0,face="plain"),
                          axis.title.y = element_text(colour="grey40",size=16,angle=90,hjust=.5,vjust=.5,face="plain"),
                          plot.title = element_text(size = 24,vjust=4, face="bold"),
                          plot.subtitle = element_text(vjust=2, size = 16),
                          plot.caption = element_text(vjust=2, size = 16),
                          legend.title = element_text(colour="grey40",size=14,hjust=.5,vjust=.5,face="bold"),
                          legend.text = element_text(colour="grey10", size=18, face="plain"),
                          strip.text.x = element_text(size = 18, angle = 0),
                          strip.text.y = element_text(size=14, face="bold"),
                          legend.position = "none") +
                    scale_y_continuous(label=comma, limits = c(0,50000)) +
                    scale_x_continuous(label=comma, limits = c(0,100)) +
                    labs(title = tt10, subtitle = stt10, caption = cptn,
                         x = "pulgadas \n", y = "precio en S/. \n") +
                    scale_color_discrete(name="marcas de tvs") +
                    geom_smooth()


ggplotly(pulgadas_precio, tooltip=c("marca", "pulgadas", "precio_actual"))
奥马尔·冈萨雷斯

我发现您可以在中使用text参数aes,并执行以下操作:

ggplot(tail_tvs, aes(x = pulgadas, y = precio_actual, text = sprintf("S/ %s", comma(precio_actual))))

注意,我正在格式化此文本参数中S/零件和comma separetorfor thousends。

现在,当调用ggplotly时,

ggplotly(pulgadas_precio, tooltip=c("marca", "pulgadas", "precio_actual"))

您称自己为文本参数,而不是原始变量:

ggplotly(pulgadas_precio, tooltip=c("marca", "pulgadas", "text"))

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在R plotly工具提示中设置变量名称的格式?

ggplotly工具提示中的日期格式

在 R/plotly 中,如何更改工具提示數字格式?

在ggplotly中格式化轴和工具提示上的数字

如何使用 R 中的填充条形图控制 ggplotly 工具提示中的“计数”

使用百分比的 R ggvis 格式工具提示

使用 ggplotly 进行缩放后的不同工具提示

在悬停时在图像上设置工具提示

在主干木偶布局上设置工具提示

通过 R 使用 plotly 创建的 choropleth 的自定义工具提示/悬停文本

如何设置货币格式

如何在饼图chartJS中将工具提示格式化为货币?

ggplotly中设置了工具提示参数时,geom_line无法绘制

ggplotly-仅在某些geom对象上返回工具提示悬停文本

工具提示删除ggplotly的回归线

ggplotly:在悬停时重命名工具提示

显示 NA 值的 ggplotly 工具提示

工具提示无法在“提交”按钮上使用

更改C3.js图表上的工具提示格式

有没有一种方法可以在R中使用ggplotly将bin范围标签添加到直方图的工具提示中?

使用C#在Excel中设置货币格式

带有工具提示的ggplotly在使用geom_rect()时遇到问题

在折线图的特定位置添加点或点,并使用 ggplotly() 相应地调整工具提示

使用ggplotly时如何选择要在工具提示中显示的变量?

在hybris中设置货币格式

动态设置货币列的格式

Highcharter R中工具提示和轴的日期格式

如何在JavaFX窗格上设置工具提示?

如何在JPanel及其子级上设置工具提示?