How to display plot values with comma separated on ggplotly ?

Reprex from this website:

library(tidyverse)
library(plotly)
# Create data frame
df <- data.frame(State = state.name,
                 LifeExp = state.x77[,4],
                 Population = state.x77[,1],
                 stringsAsFactors = F) %>%
  remove_rownames()

# Create ggplot
p <- ggplot(df, aes(x = LifeExp, y = Population, label = State)) +
  geom_point(color = 'tomato', size = 2.5) +
  theme_minimal() +
  scale_x_continuous(labels = scales::comma) +
  scale_y_continuous(labels = scales::comma) +
  labs(x = 'Life Expectancy (years)',
       y = 'Population (thousands)',
       title = 'US State Population and Life Expectancy',
       subtitle = 'Life expectancy 1969-1971; Population estimate as of July 1, 1975')

ggplotly(p) %>%
  layout(title = list(text = paste0('US State Population and Life Expectancy',
                                    '<br>',
                                    '<sup>',
                                    'Life expectancy 1969-1971; Population estimate as of July 1, 1975',
                                    '</sup>')))

However, comma is not seen on the values !!! How can we fix this ?

You can control the content and formatting of the tooltip by using a text aesthetic in the ggplot call. This is ignored by ggplot but then used by ggplotly when rendering the tooltip. See here for more details:

For the example you include, here is some code that will create a custom tooltip. See the addition of geom_point(aes(text = ...)) to specify the labels, as well as ggplotly(p, tooltip = c("text")), to let plotly know to use the text parameter to create the tooltips. I also show that you can include HTML in the tooltip text, so I've made the state name bold.

library(tidyverse)
library(plotly)

# Create data frame
df <- data.frame(State = state.name,
                 LifeExp = state.x77[,4],
                 Population = state.x77[,1],
                 stringsAsFactors = F) %>%
  remove_rownames()

# Create ggplot
p <- ggplot(df, aes(x = LifeExp, y = Population, label = State)) +
  geom_point(
    aes(
      text = paste0(
        "<b>", State, "</b>", "<br>",
        "Life Expectancy: ", LifeExp, "<br>",
        "Population: ", scales::comma(Population, 1), "<br>"
        )
    ),
    color = 'tomato',
    size = 2.5,
    ) +
  theme_minimal() +
  scale_x_continuous(labels = scales::comma) +
  scale_y_continuous(labels = scales::comma) +
  labs(x = 'Life Expectancy (years)',
       y = 'Population (thousands)',
       title = 'US State Population and Life Expectancy',
       subtitle = 'Life expectancy 1969-1971; Population estimate as of July 1, 1975')
#> Warning: Ignoring unknown aesthetics: text

ggplotly(p, tooltip = c("text")) %>%
  layout(title = list(text = paste0('US State Population and Life Expectancy',
                                    '<br>',
                                    '<sup>',
                                    'Life expectancy 1969-1971; Population estimate as of July 1, 1975',
                                    '</sup>')))

Created on 2020-01-03 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.