Date Format in Tooltip of Ggplotly

date format in tooltip of ggplotly

If you use just text in your tooltip, you can render a more complex tooltip by using the text element you pass to ggplot. You just need to call as.Date and use some <br> html tags as follows:

# draw the line plot using ggplot
gg <-ggplot(plotbycity, aes(x = date, y = rent, group = city, color = city,
text = paste('Rent ($):', rent,
'<br>Date: ', as.Date(date),
'<br>Obs: ', count))) +
geom_line() +
ggtitle("Monthly Rents")

p <- ggplotly(gg, tooltip = c("text"))

Hope that helps!

Date format in hover for ggplot2 and plotly

We can use the "hidden" text aes, to use it in the tooltip:

ggplot(datosO3.melt) +
geom_line(aes(x = fecha,
y = value,
colour = variable,
group = variable,
text = paste('fecha: ', fecha, '\n',
'variable: ', variable, '\n',
'value: ', value, '\n')
)
)

ggplotly(tooltip = 'text')

Sample Image

However for anything that's slightly more complicated than default, especially when working with hover tooltips I usually prefer to work directly in plotly:

plot_ly(datosO3.melt, 
type = 'scatter',
mode = 'lines',
x = ~fecha,
y = ~value,
color = ~variable,
text = ~paste('fecha: ', fecha, '\n',
'variable: ', variable, '\n',
'value: ', value, '\n'),
hoverinfo = 'text'
)

Sample Image

To use a custom date format, other the print.Date default, just substitute fecha with the format you prefer, e.g:

plot_ly(datosO3.melt, 
type = 'scatter',
mode = 'lines',
x = ~fecha,
y = ~value,
color = ~variable,
text = ~paste('fecha: ', format(fecha, '%Y-%m-%d %H:%M'), '\n',
'variable: ', variable, '\n',
'value: ', value, '\n'),
hoverinfo = 'text'
)

ggplotly showing numbers instead of date labels

Looks like the date class gets dropped when using stat="count". Hence, one option to achieve your desired result would be to aggregate your dataset before passing it to ggplot using e.g. dplyr::count(Flow, date, transporte):

Flow <- dplyr::count(Flow, date, transporte, name = "count")

ggplot() +
geom_line(data = Flow, aes(x = date, y = count, color = transporte)) +
scale_x_date(
date_minor_breaks = "1 month",
date_labels = "%Y (%b)"
) +
labs(color = "Type of Flow") +
ggtitle("Number of Entrances, by Month and Mode of Transportation, 2017-2021") +
xlab("Date") +
ylab("Number or People")
ggplotly()

Sample Image

A second option which additionally for setting the date format would be make use of the text "aesthetic" and convert your numbers back to proper dates:

library(plotly)

ggplot() +
geom_line(data = Flow, aes(x = date, color = transporte, text = paste(
"count:", ..count..,
"<br>Date: ", format(as.Date(..x.., origin = "1970-01-01"), "%Y (%b)"),
"<br>transporte: ", ..color..
)), stat = "count") +
scale_x_date(
date_minor_breaks = "1 month",
date_labels = "%Y (%b)"
) +
labs(color = "Type of Flow") +
ggtitle("Number of Entrances, by Month and Mode of Transportation, 2017-2021") +
xlab("Date") +
ylab("Number or People")
ggplotly(tooltip = c("text"))

Sample Image

In R/plotly, how to change the tooltip number format?

You may try using scales::percent

library(scales)
p <- mydata %>%
mutate(sales = scales::percent(sales)) %>%
ggplot(aes(x=category,y=sales))+
geom_point()

ggplotly(p,tooltip=c('y','x'))

Sample Image

Format numbers on axes and tooltips in ggplotly

This can be easily achieved via scales::percent or scales::percent_format. As an example I made a line plot of Percent by Date where I use scales::percent_format to format y-axis labels and scales::percent to format the percent value in the tooltip:

library(ggplot2)
library(plotly)

p <- df %>%
ungroup() %>%
ggplot(aes(Date, Percent, color = State, group = State,
text = sprintf("State: %s<br>Date: %s<br>Percent: %s",
State, Date, scales::percent(Percent, scale = 1, accuracy = .01)))) +
geom_line() +
scale_y_continuous(labels = scales::percent_format(scale = 1, accuracy = .01))

ggplotly(p, tooltip = 'text')

Sample Image

R: format currency on tooltip using ggplotly or plotly

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

Sample Image



Related Topics



Leave a reply



Submit