【问题标题】:Quirk with geom_bar and ggplotlygeom_bar 和 ggplotly 的怪癖
【发布时间】:2017-01-22 04:36:17
【问题描述】:

我在 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 时,这些值似乎不会被强制转换。我错过了什么吗?

感谢您的帮助。

【问题讨论】:

标签: r ggplot2 plotly


【解决方案1】:

你可以试试这个:

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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多