简体   繁体   English

ggplotly 无法识别来自 R 中 ggplot 的 geom_rect 填充

[英]ggplotly not recognizing geom_rect fill from ggplot in R

I am trying to determine how to ensure that the fill of a geom_rect in ggplot2 is respected once wrapped in plotly::ggplotly() .我试图确定如何确保在geom_rectggplot2fill一旦被包裹在plotly::ggplotly()就受到尊重。

Example:例子:

I first create a data.frame that contains the values I'll use to generate my plot.我首先创建一个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)
           )

Using just ggplot the background panel colors on the geom_rect are respected仅使用geom_rect即可尊重ggplot的背景面板颜色

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)

在此处输入图片说明

But, when I wrap it with ggplotly , those background colors disappear.但是,当我用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))

在此处输入图片说明

Any thoughts?有什么想法吗? I'm not super familiar with all the intricacies of plotly , so any insights are helpful!我不太熟悉plotly所有复杂性,所以任何见解都是有帮助的!

Not sure how to get this automatically, but one workaround that ggplotly bug is to use specific numbers in place of -Inf and Inf:不确定如何自动获取此信息,但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))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM