我正在制作图表,首先在ggplot中进行绘制,然后使其与ggplotly进行交互。
但我需要正确格式化价格的工具提示(在data.frame中称为“ precio_actual”)。
在工具提示中显示为:1499。
应为:S / 1,4900.00。
资料:
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 separetor
for thousends。
现在,当调用ggplotly时,
ggplotly(pulgadas_precio, tooltip=c("marca", "pulgadas", "precio_actual"))
您称自己为文本参数,而不是原始变量:
ggplotly(pulgadas_precio, tooltip=c("marca", "pulgadas", "text"))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句