首页 > 解决方案 > 如何添加美元符号以悬停?

问题描述

这是我正在使用的代码:

library(ggplot2)
library(plotly)

 ggplotly(ggplot(economics_long, aes(date, value)) +
      geom_line() +
      facet_wrap(vars(variable), scales = "free_y", ncol = 1, strip.position = "top") +
      theme(strip.background = element_blank(), strip.placement = "outside"), hoverinfo = "text", hovertext = "value: %{value:$.2f}<br>")

当您将鼠标悬停在图表上时,我试图在“值”之前包含美元符号。我现在在代码中所做的似乎不起作用。有人知道我在做什么错吗?

例如:价值:4851.20 美元

标签: rggplot2plotlyggplotly

解决方案


library(ggplot2)
library(plotly)

ggplotly(
  ggplot(
    economics_long, 
    aes(
      date, 
      value, 
      group = 1,
      text = paste0("value: $", sprintf("%.2f", value))
    )
  ) +
    geom_line() +
    facet_wrap(vars(variable), scales = "free_y", ncol = 1, strip.position = "top") +
    theme(strip.background = element_blank(), strip.placement = "outside"), 
  tooltip = "text"
)

推荐阅读