使用R

时间:2019-06-26 17:08:46

标签: r plotly gauge

我已经多次看到这个问题,但似乎没有一个解决方案对我有用。我是R的新手,因此不一定了解所有这些图表的来龙去脉。

我正在尝试使用plotly在R Cloud中创建一个量表,并且正在尝试根据数据中的当前值更改指针。

我查看了以下已发布的问题:

Dial Position Gauge Chart Plotly R

How to rotate the dial in a gauge chart? Using python plotly

他们的解决方案中有很多数字尚未定义(例如180和300)。我已经尝试过将它们保持原样,或者试图猜测它们的含义(以防万一我需要更改它们以适合我的数据),但是它们都不起作用。

量规图呈现正确,但指针位置不正确。我希望它指向2000年标记。

library(plotly)
library(Math)
current <- 2000
goal <- 10000

base_plot <- plot_ly(
  type = "pie",
  values = c(40, 10, 10, 10, 10, 10, 10),
  labels = c("-", "0", goal*.2, goal*.4, goal*.6, goal*.8, goal),
  rotation = 108,
  direction = "clockwise",
  hole = 0.4,
  textinfo = "label",
  textposition = "outside",
  hoverinfo = "none",
  domain = list(x = c(0, 0.48), y = c(0, 1)),
  marker = list(colors = c('rgb(255, 255, 255)', 'rgb(255, 255, 255)', 'rgb(255, 255, 255)', 'rgb(255, 255, 255)', 'rgb(255, 255, 255)', 'rgb(255, 255, 255)', 'rgb(255, 255, 255)')),
  showlegend = FALSE)

a <- list(showticklabels = FALSE, autotick = FALSE, showgrid = FALSE, zeroline = FALSE)
b <- list(xref = 'paper', yref = 'paper', x = 0.23, y = 0.45, showarrow = FALSE, text = current)
h <- .24
k <- .5
r <- .15
theta <- ((100 - current) * 180 / 100) * pi / 180
    I've also tried: 
    theta <- pi/180 * (goal - current) / goal
    theta <- (current * 180 / 300) * pi / 180
c <- h + r * cos(theta)
    I've also tried
    c <- h - r * cos(theta)
d <- k + r * sin(theta)

base_chart <- layout(base_plot,
  shapes = list(list(
      type = 'path',
      path = paste('M .235 .5 L',c,d,'L 0.245 0.5 Z'),
      xref = 'paper',
      yref = 'paper',
      fillcolor = 'rgba(44, 160, 101, 0.5)')),
  xaxis = a, yaxis = a, annotations = b)```

0 个答案:

没有答案