【发布时间】:2020-01-02 17:31:00
【问题描述】:
我在定义 x 轴的日期范围时遇到了 plotly 条形图的问题。
当有一个或多个数据点具有相同的 x 值时,条形图不会显示在图中。如果至少有两个不同的 x 值,或者如果我不使用 x 轴范围,则条形将按应有的方式显示。
下面是一个例子(我目前使用lubridate来处理日期)。
library(lubridate)
library(plotly)
# Same x-value: bar does not show
plot_ly(x = c(ymd("2019-08-25"), ymd("2019-08-25")), y = c(1, 2), type = "bar") %>%
layout(xaxis = list(range = ymd(c("2019-08-20", "2019-08-30"))))
# Different x-values: bars are shown
plot_ly(x = c(ymd("2019-08-25"), ymd("2019-08-26")), y = c(1, 2), type = "bar") %>%
layout(xaxis = list(range = ymd(c("2019-08-20", "2019-08-30"))))
# No x-axis range defined, same x-values: the bar is shown
plot_ly(x = c(ymd("2019-08-25"), ymd("2019-08-25")), y = c(1, 2), type = "bar")
有什么办法吗?
编辑:为了比较,ggplot2 没有同样的问题:
# ggplot works like expected
library(lubridate)
library(ggplot2)
ggplot(NULL, aes(x = ymd(c("2019-08-25", "2019-08-25")), y = c(1, 2))) +
geom_col() +
xlim(ymd(c("2019-08-20", "2019-08-30")))
【问题讨论】:
-
你想在这里实现什么?当同一条有 2 个值时,它们必须加起来为一个条吗?还是您希望 2 个条相互堆叠?
-
@Arcoutte 它们将作为默认值相加(如示例 3 中所示)。如果数据点具有与
color参数绑定的第三个特征,它们将堆叠,在这种情况下,我使用layout(barmode = "stack", ...)。但我选择为这个问题使用一个最小的工作示例。