Skip to content

ggplotly with geom_rect not displaying geom_bar and not following ggplot layer order #1750

@iwontrememberthis

Description

@iwontrememberthis

I have a ggplot where I would like to have a striped background of grey and white. I have achieved this using geom_rect, as can be seen below:

ggplot(growth.mon, aes(x = Month, y = Rate)) +
        geom_rect(ymin = 0.3, ymax = 0.4, 
                  xmin = 0, xmax = 1000000, fill = '#fbfcfc') +
        geom_rect(ymin = 0.2, ymax = 0.3,
                  xmin = 0, xmax = 1000000, fill = '#f5f6f9')+
        geom_rect(ymin = 0.1, ymax = 0.2, 
                  xmin = 0, xmax = 1000000, fill = '#fbfcfc')+ 
        geom_rect(ymin = 0, ymax = 0.1, 
                  xmin = 0, xmax = 1000000, fill = '#f5f6f9')+
        geom_rect(ymin = -0.1, ymax = 0, 
                  xmin = 0, xmax = 1000000, fill = '#fbfcfc')+
        geom_rect(ymin = -0.2, ymax = -0.1, 
                  xmin = 0, xmax = 1000000, fill = '#f5f6f9')+
        geom_rect(ymin = -0.3, ymax = -0.2, 
                  xmin = 0, xmax = 1000000, fill = '#fbfcfc')+
        geom_bar(stat = "identity", aes(fill = as.factor(1)), show.legend = FALSE)+
        geom_line(aes(y = rollMean, colour = "#7f5ba2"), size = 1.1, show.legend = FALSE)+
        scale_fill_manual(values = c("#0095db"))+
        scale_colour_manual(values = c("#7f5ba2"))+
        scale_y_continuous(NULL, labels = percent_format())+
        scale_x_date(date_breaks = "1 month", date_labels =  "%b %Y")+
        theme(axis.text.x=element_text(angle=60, hjust=1))+
        theme(legend.position = "none")

Which creates this

Now I am developing a shiny app and I would like this plot to be interactive rather than static, so I use ggplotly like so:

ggplotly(gg_growth)

Data:

dates <- seq(as.Date("2017-02-01"), length = 36, by = "1 month") - 1

sales_mon17 <- c(1503, 1563, 1434.5,1807, 1843.7, 1664, 1285, 1188, 1513, 1997,1718.2, 2191)

sales_mon18 <- c(1919, 1886, 1995, 1930, 1898, 2122, 1818, 1908, 1974, 2074, 1700, 2303)

sales_mon19 <- c(2319, 2424, 2353, 2474, 2500, 2538, 2444, 2219, 1908, 2404, 2288, 3079.7)

monthly_revenue <- data.frame(Month = dates, Revenue = c(sales_mon17, sales_mon18, sales_mon19))

growth.mon <- diff(monthly_revenue$Revenue) / lag(monthly_revenue$Revenue)[-1]

growth.mon <- data.frame(Month = monthly_revenue$Month[-1], Rate = growth.mon)

growth.mon$rollMean <- c(NA, NA, rollmean(growth.mon$Rate, 3))

Unfortunately, the geom_rect get plotted over the geom_bar, which you can see here

Why doesn't plotly follow the order of ggplot layers? That is the geom_rect come first and hence the geom_bar should go over the geom_rect. This is the normal and intended behaviour of ggplot and a big part of why it is so powerful, so why doesn't plotly follow this in ggplotly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions