1

I'm making a plot, first in ggplot and then making it interactive with ggplotly.

But I need that the tooltip for currect price, called "precio_actual" in data.frame to be correctly formated.

It appears as: 1499 in tooltip.

Should be: S/ 1,4900.00.

I'm reading the documentation for ggplotly, but cannot find any indication on how to achieve this.

data:

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))

code:

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"))

2 Answers 2

7

Unfortunately your code doesn't run, so I can't write a solution for you and test it, BUT...

You will want to pass a formatted string as presented in this StackOverflow post.

For special currency formatting, I recommend writing your own function e.g. mycurrency(x) that returns a formatted string of the value you pass it. Use it as proposed below.

# 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"))

See below an example of a simple currency formatting method (inspired by this post)

mycurrency <- function(x){
  return(paste("$", formatC(as.numeric(x), format="f", digits=2, big.mark=",")))
}
Sign up to request clarification or add additional context in comments.

Comments

2

I've discovered you can use a text argument in aes, and do the following:

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

Notice I'm formatting the S/ part and the comma separetor for thousends in this text argument.

Now when calling ggplotly, instead of

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

You call you text argument, instead of the original variable:

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

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.