Search code examples
rformattingplotly

R Plotly - Format Currency


For a shiny app, I would like to plot a pie chart using plotly. The data consists of prices in the hundred thousands. In the chart I want to print the labels and values. Since I am from Germany, I would like the prices to be in € and the thousand separators to be dots.

I managed to print the labels and to format the prices as a currency using texttemplate as shown below:

library(plotly)

plot_ly(diamonds, labels = ~cut, values = ~price, type = 'pie',
        texttemplate = "%{label} <br> %{value:$,}")

However, I fail to change the currency to € and the separators to dots. I believe that in JAVA this can be achieved by setting the location. Unfortunately, I don't know how to do this in R.

Thank you very much in advance!


Solution

  • You could use the unicode \U20AC of the euro sign like this:

    library(plotly)
    plot_ly(diamonds, labels = ~cut, values = ~price, type = 'pie',
            texttemplate = "%{label} <br> \U20AC%{value:,}")
    

    Created on 2023-02-01 with reprex v2.0.2

    To change separator to dots you can use layout with separators like this:

    library(plotly)
    plot_ly(diamonds, labels = ~cut, values = ~price, type = 'pie',
            texttemplate = "%{label} <br> \U20AC%{value:,}") %>%
      layout(separators = ",.")
    

    Created on 2023-02-02 with reprex v2.0.2