I'm trying to add a plot to my shiny dashboard. The y-axis variable is a proportion and I want to format this as a percentage. I've discovered I can do it using the tickformat feature in the yaxis setting of the layout. However, since the percentages in my chart are all so low, I've noticed a couple of quirks.
Firstly, both the y-axis and the actual values when you hover over the data points are rounded to 0 decimal places. So both 0.0287 and 0.275 appear as 0.3 for example, even though their actual position on the chart is slightly different, reflecting the true underlying value.
Secondly, when the y-axis range is so small and is also round to 0 decimal places by default, it shows the same value at multiple ticks, and the tick intervals are also not intuitive.
I have provided example code below, adapted from the example on the plotly site. (I just changed the values to smaller values).
You can see the chart it produces gives tick marks show 2%, 3%, 3%, 4%, 4%, 5%, 5%, 5%, 6%. First problem with this is it's misleading, secondly you can see there's 3x 5% there, but only 2x of the others, so it seems like it's not even doing a sensible gap like 4.5%, 5%, 5.5%, etc., but rather something unintuitive based on the data values.
library(plotly)
fig <- plot_ly(
type = "scatter",
x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
y = c(0.0185, 0.0382, 0.0561, 0.0463, 0.0596, 0.048, 0.0287, 0.0275, 0.0243, 0.0429, 0.0450, 0.0391),
mode = "markers+lines")
fig <- fig %>%
layout(
yaxis = list(
tickformat = "%"
))
fig
In summary:
How do I...
- Specify how to round the percentages?
- Ensure that it doesn't space tick marks at such an interval that the same rounded value appears more than once?
- Ensure that it chooses an intuitive tick interval when looking at decimal places (such as 0.5 or 0.25, rather than something silly like 0.37 for example)