I am trying to determine how to ensure that the fill
of a geom_rect
in ggplot2
is respected once wrapped in plotly::ggplotly()
.
Example:
I first create a data.frame
that contains the values I'll use to generate my plot.
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
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(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!
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(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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.