带有单个观察结果的浅色色条

时间:2019-06-24 12:51:24

标签: r bar-chart plotly colorbar

我正在尝试绘制一个条形图,其中根据第三个“概率”变量为条形着色。我想将颜色条设置为固定为0:100,并绘制数据的不同子集。一切正常,直到我的子集只包含一个观察值,然后颜色不再跟随颜色条。也许有人可以指出我正确的方向? 示例:

> data <- data.table(name = c("foo", "bar", "bar", "bar"), value = c(10, 15, 40, 35), probability = c(0.9, 0.4, 0.8, 0.3), id = c("one", "two", "three", "four"))
> 
> data
   name value probability    id
1:  foo    10         0.9   one
2:  bar    15         0.4   two
3:  bar    40         0.8 three
4:  bar    35         0.3  four
> 
> data1 <- data[name == "bar"]
> data2 <- data[name == "foo"]
> 
> p1 <- plot_ly(data1,
+        x = ~value,
+        y = ~id,
+        type = "bar",
+        orientation = "h",
+        marker = list(color = ~(probability * 100), colorbar = list(title = "Probability", cmin = 0, cmax = 100))
+ )
> 
> p2 <- plot_ly(data2,
+         x = ~value,
+         y = ~id,
+         type = "bar",
+         orientation = "h",
+         marker = list(color = ~(probability * 100), colorbar = list(title = "Probability", cmin = 0, cmax = 100))
+ )
> 
> plotly::subplot(p1, p2)

这是结果图:

2 个答案:

答案 0 :(得分:0)

我认为movePaddle :: PongGame -> PongGame movePaddle = moveLeftPaddle . moveRightPaddle moveLeftPaddle game | (wHeld game) = game {playerLPos = paddleUp (playerLPos game)} | (sHeld game) = game {playerLPos = paddleDn (playerLPos game)} | otherwise = game moveRightPaddle game | (upHeld game) = game {playerRPos = paddleUp (playerRPos game)} | (downHeld game) = game {playerRPos = paddleDn (playerRPos game)} | otherwise = game paddleUp pos = min (pos + 10) paddleMax paddleDn pos = max (pos - 10) paddleMin cmincmax的属性,而不是Marker的属性,因此请更改

Colorbar

marker = list(
    color = ~(probability * 100),
    colorbar = list(title = "Probability", cmin = 0, cmax = 100)
)

应该工作。

答案 1 :(得分:0)

你好,完全陌生的人,

我认为vlizana暗示Rplotly实际上存在一个错误。 如果只有一行,则图例无法正常运行。因此,如果在这种情况下添加新行,它将起作用。并且,如果该行具有NA而不是值,您将收到警告,但不会出现在图中。

实现此目的的代码如下:

data <- data.table(name = c("foo", "bar", "bar", "bar"), value = c(10, 15, 40, 35), probability = c(0.9, 0.4, 0.8, 0.3), id = c("one", "two", "three", "four"))

data1 <- data[name == "bar"]
data2 <- data[name == "foo"]
if(nrow(data2)== 1){
  add_data2 <- data2[2L] 
  add_data2[,1] <- "Delete"
  data2 <- rbind(data2, add_data2)
}

p1 <- plot_ly(data1,
              x = ~value,
              y = ~id,
              type = "bar",
              orientation = "h",
              marker = list(color = ~probability * 100 , colorbar = list(title = "Probability", cmin = 0, cmax = 100, cauto = F))
)
p2 <- plot_ly(data2,
              x = ~value,
              y = ~id,
              type = "bar",
              orientation = "h",
              marker = list(color = ~probability * 100, colorbar = list(title = "Probability", cmin = 0, cmax = 100, cauto = F))
)
plotly::subplot(p1, p2)