怪癖 geom_bar 和 ggplotly

Quirk with geom_bar and ggplotly

我发现我认为 R 的 plotly 包中的 ggplotly 函数有一个怪癖。

当试图在 ggplotly 函数内包装 ggplot 图(使用 geom_barstat = identity)时,负值被强制转换为正值。

以下是玩具示例:

library(ggplot2)
library(plotly)

set.seed(12345)

x <- data.frame(
    x = 1:10,
    obs = floor(rnorm(10) * 100)
)

# x  obs
# 1   58
# 2   70
# 3  -11
# 4  -46
# 5   60
# 6 -182
# 7   63
# 8  -28
# 9  -29
# 10  -92

test_plot <- ggplot(x, aes(factor(x), obs)) + geom_bar(stat = "identity")
test_plot

ggplotly(test_plot)

使用其他 geoms 时,这些值似乎没有被强制转换。我错过了什么吗?

感谢您的帮助。

你可以试试这个:

library(plotly)
set.seed(12345)

df <- data.frame(
  x = as.factor(1:10),
  obs = floor(rnorm(10) * 100)
)
plot_ly(x = df$x, y = df$obs, type = 'bar', name = 'Plotly') %>%
  layout(xaxis = list(title = 'x'), yaxis = list(title = 'obs'), barmode = 'group')