R 如何计算绘图仪中的指针位置

R 如何计算绘图仪中的指针位置,r,plotly,R,Plotly,我正在尝试使用R/Shiny中的plotly创建一个仪表图,我很难弄清楚如何根据数值确定指针点的(x,y)坐标 我的仪表最小值为18792,最大值为29472,中点为24132。我使用以下代码计算(x,y)坐标: 我假设圆的原点是(0.5,0.5)。我需要针的长度约为0.2(因此r=0.2) 正如预期的那样,每当达到最小值(18792)时,打捆针位置为(0.3,0.5),并且在屏幕上查看时,看起来正常: 但是,当该值达到中点(24132)时,(x,y)达到预期值(0.5,0.7),指针似乎没有

我正在尝试使用R/Shiny中的plotly创建一个仪表图,我很难弄清楚如何根据数值确定指针点的(x,y)坐标

我的仪表最小值为18792,最大值为29472,中点为24132。我使用以下代码计算(x,y)坐标:

我假设圆的原点是(0.5,0.5)。我需要针的长度约为0.2(因此r=0.2)

正如预期的那样,每当达到最小值(18792)时,打捆针位置为(0.3,0.5),并且在屏幕上查看时,看起来正常:

但是,当该值达到中点(24132)时,(x,y)达到预期值(0.5,0.7),指针似乎没有上一示例中的那么长:

我使用的代码如下。您可以通过注释出适当的county值在min和mid之间切换:

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 <- 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 <- 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 <- 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 <- 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('<b>', county_label, '</b>')
    )
  )
)

basePlot
max=29472
最小值=18792
加州=23364
县=24132
#县=18792
min_label=paste0($),格式(min,big.mark=“,”)
max_label=paste0($),格式(max,big.mark=“,”)
california_label=paste0(“$”,格式(california,big.mark=“,”)
county_label=paste0($),格式(county,big.mark=“,”)
#定义针点
每度=(最大-最小)/180
degs=圆形((县-分钟)/每度,位数=0)

deg2rad我知道这有点过时了,但我刚刚遇到了完全相同的问题,但是使用了pythonapi。我不需要我的量具绝对精确,“足够接近”就可以了。我通过将y轴上的半径值加倍来解决此问题


这产生了可接受的结果,我认为这是因为plotly将x和y轴都视为介于0和1.0之间的值。我使用的是一个矩形显示面板,这意味着x=0.1和y=0.1是不同的度量。希望这对其他人有帮助
p1**对于Python-我也有同样的问题,但只使用Python

在我的例子中,这是因为半圆实际上不是半圆。如果您使用

fig.update_layout(xaxis={'showgrid': True, 'showticklabels':True})
您可能会发现沿x&y轴的半圆半径不同。为了解决这个问题,我找到了另一个参数并添加了另一行:

fig.update_layout(autosize=False)
再次绘制网格以进行检查,但对我来说,这使我的仪表沿x和y轴的半径相同。然后,我的箭头/指示器在四周都是相同的长度,并且指向准确。另一个潜在的技巧是,如果你不想让你的箭一直延伸到外缘,你可以使用这个参数:

standoff=30 #try numbers to see what works
将此参数放在箭头注释中,并使用一些值进行处理

@TWCars:您可以在图表的任意位置手动添加文本,行如下:

fig.add_annotation(x=-.5, y=.5, text="<b>Text</b>", showarrow=False)
fig.add_注释(x=-.5,y=.5,text=“text”,showarrow=False)
其中x和y值是文本将显示在网格上的位置(b标记为粗体,可以删除)。这有点粗糙,但如果您的图形是静态的,并且需要文本始终位于一个位置,那么这可能是一个很好的解决方案

fig.add_annotation(x=-.5, y=.5, text="<b>Text</b>", showarrow=False)