我要先在ggplot中绘制一个图,然后再使其与ggplotly进行交互。
但是我需要正确格式化价格的工具提示,在data.frame中称为“ precio_actual”。
在工具提示中显示为:1499。
应:S / 1,4900.00。
我正在阅读documentation for 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"))
答案 0 :(得分:2)
很遗憾,您的代码无法运行,所以我无法为您编写解决方案并对其进行测试,但是...
您将希望传递一个格式字符串,如 this StackOverflow post。
对于特殊的货币格式,建议您编写自己的函数,例如mycurrency(x)返回您传递的值的格式字符串。按照下面的建议使用它。
# draw the line plot using ggplot
gg <-ggplot(plotbycity, aes(x = date, y = rent, group = city, color = city,
text = paste('Rent:', mycurrency(rent),
'<br>Date: ', as.Date(date),
'<br>Obs: ', count))) +
geom_line() +
ggtitle("Monthly Rents")
p <- ggplotly(gg, tooltip = c("text"))
请参阅以下示例,了解一种简单的货币格式设置方法(受this post启发)
mycurrency <- function(x){
return(paste("$", formatC(as.numeric(x), format="f", digits=2, big.mark=",")))
}
答案 1 :(得分:1)
我发现您可以在text
中使用aes
参数,并执行以下操作:
ggplot(tail_tvs, aes(x = pulgadas, y = precio_actual, text = sprintf("S/ %s", comma(precio_actual))))
注意,我正在格式化此文本参数中S/
部分和comma separetor
的数字。
现在调用ggplotly时代替
ggplotly(pulgadas_precio, tooltip=c("marca", "pulgadas", "precio_actual"))
您称自己为文本参数,而不是原始变量:
ggplotly(pulgadas_precio, tooltip=c("marca", "pulgadas", "text"))