How can I align text to the left and numbers to the right in the ggplotly tooltip?

Viewed 62

I am creating a plotly interactive object and am wondering how to align text to the left and numbers to the right?

Here is a small snippet of code

sales <- data.frame(names = c("Joey","Adam","Paul"),
                    expenses = c(10056.244,17621.901,13090.782),
                    revenue = c(20161252367.729,1922363266239.341,24091234670.048))


ggplot(sales, aes(x = expenses, y = revenue)) +
  geom_point(aes(text = paste0("Expenses: $",format(round(expenses,2), nsmall = 1, big.mark = ","),
                               "\nRevenue: $",format(round(revenue,2), nsmall = 1, big.mark = ","))
                 )) +
  stat_smooth(method = "lm", level = 0.1,
              size = 0.25, color = "red")

ggplotly(tooltip = "text") %>% 
  layout(hoverlabel = list(align = "right"))

Right now the tooltip produces this output img

I would like to align the text "Revenue: " to the left and then the number to the right. I have tried supplying the align function a vector to do the operations in order but that hasn't worked either. I have a feeling I won't be able to use paste0 to do this, so if that's the case, how would I solve this problem? This is important, because it is much easier to compare financial numbers if they are right aligned.

Bonus points: I would also like to create a label for hovertext for the regression line if possible, but the aligning is probably more important.

0 Answers
Related