4

I have a 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")

This creates this: plot

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

ggplotly(gg_growth)

However, the chart ends up removing the bars like this:

this

Can someone tell me what went wrong and how to fix this, please? Thank you.

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))
massisenergy
  • 1,764
  • 3
  • 14
  • 25
  • I don't have an answer, but it appears plotly is not honoring the sequential order of how the layers are added. all the calls to geom_rect() are "on top of" geom_bar(). If you comment out one of the geom_rect() calls, you will see the bars 'poking' out from underneath. Plotly does honor alpha, so you could add "alpha = .1" to all the geom_rects() – Chase Apr 21 '20 at 16:54
  • You are right about the layering. The setting the alpha to 0.1 defeats the purpose of what I am trying to achieve, which is a striped back ground. However, with alpha set to 0.6, I notice that the bars are _behind_ the geom_rects but the geom_line is in front of both. Any idea as to why this is the case? Also, do you know another way to create a striped background or two tone/ two colour background in ggplot? –  Apr 21 '20 at 17:26
  • Yeah I understand the goal...making alpha transparent gets you part of the way there. Without digging into the internals of plotly, I don't think you're going to find a satisfying answer. This seems like a bug that you could report to the plotly team. You get the same behavior if you change geom_line() to geom_bar() in this example from their documentation: https://plotly.com/ggplot2/geom_rect/. Similar question here: https://stackoverflow.com/questions/50887710/ggplotly-with-tooltip-has-problems-using-geom-rect – Chase Apr 21 '20 at 18:29

0 Answers0