2016-04-28 48 views
2

我试图在R/Shiny中使用绘图创建量规图表,并且很难计算出如何确定(x,y)点的坐标针取决于价值。如何计算密集度量图中的针位置

我的计分= 18792和max = 29472,具有24132.我使用下面的代码来计算(X,Y)的中点坐标:

x1 = round(0.5 + (0.2 * cos(deg2rad(180-degs))), digits = 3) 
y1 = round(0.5 + (0.2* sin(deg2rad(180-degs))), digits = 3) 

我假设圆原点是(0.5,0.5)。我需要的针的长度是0.2(所以r = 0.2)。

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

enter image description here

然而,当值到达中点(24132),而(X,Y)上来如预期(0.5,0.7),针似乎并不是只要在前面的例子:

enter image description here

的我正在使用的代码如下。您可以通过评论相应的县数值在分钟和中间进行切换:

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 

必须有我完全错过的东西。任何帮助是极大的赞赏!

回答

1

我知道这是事后的一小部分,但我刚刚遇到完全相同的问题,但使用Python API。我不需要我的指令是绝对精确的,而且'够接近'就能做到。我通过在y轴上加倍半径值来解决问题。

这产生了可以接受的结果,我认为这是因为我们将x轴和y轴作为一个介于0和1.0之间的值。我使用的矩形显示面板意味着x = 0.1和y = 0.1是不同的措施。希望这可以帮助别人!