title: How to calculate needle position in plotly gauge chart
body: <p>I'm trying to create a gauge chart using plotly in R/Shiny and am having a hard time figuring out how to determine the (x,y) coordinates of the point of the needle depending on value. </p>
  
  <p>My gauge min = 18792 and max = 29472, with a mid-point of 24132. I use the following code to calculate (x,y) coords:</p>
  
  <pre><code>x1 = round(0.5 + (0.2 * cos(deg2rad(180-degs))), digits = 3)
  y1 = round(0.5 + (0.2* sin(deg2rad(180-degs))), digits = 3)
  </code></pre>
  
  <p>I'm assuming the circle origin is (0.5,0.5). I need the needle to be ~ 0.2 in length (so r = 0.2).</p>
  
  <p>As expected, whenever the minimum value is reached (18792), the needle position is (0.3, 0.5), and, reviewing it on screen, looks normal:</p>
  
  <p><a href="https://i.stack.imgur.com/val9g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/val9g.png" alt="enter image description here"></a></p>
  
  <p>However, when the value reaches mid-point (24132), while (x,y) come up as expected (0.5, 0.7), the needle doesn't seem to be as long as the previous example:</p>
  
  <p><a href="https://i.stack.imgur.com/ymATA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ymATA.png" alt="enter image description here"></a></p>
  
  <p>The code I am using is as follows. You can switch between min and mid by commenting out the appropriate county value:</p>
  
  <pre><code>max = 29472
  min = 18792
  california = 23364
  county = 24132
  # county = 18792
  
  min_label = paste0("$", format(min, big.mark = ","))
  max_label = paste0("$", format(max, big.mark = ","))
  california_label = paste0("$", format(california, big.mark = ","))
  county_label = paste0("$", format(county, big.mark = ","))
  
  
  # Define needle points
  perDeg = (max - min)/180
  
  degs = round((county - min)/perDeg, digits = 0)
  
  deg2rad &lt;- function(deg) { deg * pi / 180 }
  
  
  # Define [x2,y2] - the needle point
    x1 = round(0.5 + (0.2 * cos(deg2rad(180-degs))), digits = 3)
    y1 = round(0.5 + (0.2* sin(deg2rad(180-degs))), digits = 3)
  
  
  
  basePlot &lt;- plot_ly(
    type = "pie",
    values = c(40, 10, 40, 10),
    labels = c(" ", min_label, "  ", max_label),
    rotation = 108,
    direction = "clockwise",
    hole = 0.7,
    textinfo = "label",
    textposition = "outside",
    hoverinfo = "none",
    domain = list(x = c(0, 1), y = c(0, 1)),
    # marker = list(colors = c('rgb(100, 100, 255)', 'rgb(100, 100, 255)', 'rgb(100, 100, 255)', 'rgb(100, 100, 255)')),
    showlegend = FALSE,
    sort = FALSE
  )
  
  basePlot &lt;- add_trace(
    basePlot,
    type = "pie",
    values = c(50, 50),
    labels = c("Estimated Annual Cost of Living", " "),
    rotation = 90,
    direction = "clockwise",
    hole = 0.7,
    textinfo = "label",
    textposition = "inside",
    hoverinfo = "none",
    domain = list(x = c(0, 1), y = c(0, 1)),
    # marker = list(colors = c('rgb(255, 255, 255)', 'rgb(255, 255, 255)')),
    showlegend= FALSE
  )
  
  basePlot &lt;- layout(
    basePlot,
    shapes = list(
      list(
        type = 'path',
        path = paste0('M 0.5 0.5 L ', x1, ' ', y1, ' Z'),
        xref = 'paper',
        yref = 'paper',
        fillcolor = 'rgb(226,210,172)'
      )
    ),
    annotations = list(
      list(
        xref = 'paper',
        yref = 'paper',
        x = 0.5,
        y = 0.4,
        showarrow = FALSE,
        text = paste0('&lt;b&gt;', county_label, '&lt;/b&gt;')
      )
    )
  )
  
  basePlot
  </code></pre>
  
  <p>There has to be something that I'm totally missing. Any help is greatly appreciated!</p>