【问题标题】:ggplotly not recognizing geom_rect fill from ggplot in Rggplotly 无法从 R 中的 ggplot 识别 geom_rect 填充
【发布时间】:2021-12-07 16:13:57
【问题描述】:

我正在尝试确定如何确保在 ggplot2 中的 geom_rectfill 在包裹在 plotly::ggplotly() 中后得到尊重。

例子:

我首先创建一个data.frame,其中包含我将用于生成绘图的值。

library(ggplot2)
library(plotly)

dat <- data.frame(provider = rep(c('a','b','c'),2),
           category = c(rep(c('Inpatient'),3),rep(c('Outpatient'),3)),
           revenue = runif(6,100,500),
           background_col = rep(c('red','green','blue'),2)
           )

仅使用ggplot 会尊重geom_rect 上的背景面板颜色

ggplot(dat,aes(x=category,y=revenue)) +
              geom_rect(data = dat,aes(fill = background_col),xmin = -Inf,xmax = Inf,
                        ymin = -Inf,ymax = Inf,alpha = 0.1) +
              geom_bar(stat = 'identity') +
              facet_grid(~provider)

但是,当我用ggplotly 包裹它时,那些背景颜色消失了。

ggplotly(ggplot(dat,aes(x=category,y=revenue)) +
                          geom_rect(data = dat,aes(fill = background_col),xmin = -Inf,xmax = Inf,
                                    ymin = -Inf,ymax = Inf,alpha = 0.1) +
                          geom_bar(stat = 'identity') +
                          facet_grid(~provider))

有什么想法吗?我对plotly 的所有复杂性并不是非常熟悉,因此任何见解都会有所帮助!

【问题讨论】:

标签: r ggplot2 ggplotly


【解决方案1】:

不确定如何自动获取此信息,但ggplotly 错误的一种解决方法是使用特定数字代替 -Inf 和 Inf:

ggplotly(ggplot(dat,aes(x=category,y=revenue)) +
           geom_rect(data = dat,aes(fill = background_col),xmin = 0,xmax = 3,
                     ymin = -25,ymax = 475,alpha = 0.1) +
           geom_bar(stat = 'identity') +
           facet_grid(~provider))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-16
    • 2023-03-11
    • 2019-10-26
    • 2020-11-03
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    相关资源
    最近更新 更多