简体   繁体   中英

Using geom_rect in plotly

I have the following plot using ggplot2 and plotly . However, the plotly image does not display the shaded areas I've inserted using geom_rect . I'm guessing that this is because plotly doesn't support geom_rect , does anyone have a workaround to solve this?

Here is my code

library(rmsfuns)
packages <- c('ggplot2','ggthemes','lubridate',
              'readxl','plotly', 'dplyr','tidyr', 'scales')
load_pkg(packages)

min <- as.Date('2015-01-01')
max <- as.Date('2020-07-01')

p1 <- ggplot(pmi_data, aes(Date,Value)) +
  geom_rect(xmin=as.Date("2019-10-01"), xmax=as.Date("2020-07-01"), 
            ymin=0, ymax=70, fill="grey90", alpha=0.3, col="grey90") +
  geom_rect(xmin=as.Date("2018-01-01"), xmax=as.Date("2018-07-01"), 
            ymin=0, ymax=Inf, fill="grey90", alpha=0.3, col="grey90") +
  geom_line(size = 1, col = 'maroon') + labs(title = 'Purchasing Manufacturing Index (PMI) - South Africa',
                     subtitle = 'Sharp Bounce in 2020 due to COVID-19',
                     y = 'Index Value', caption = 'Data downloaded from Quantec,
                     Jan 2000 - Jul 2020 \n *Shaded areas indicate South African Recession Periods') +
  theme_solarized() +
  geom_hline(yintercept = 50, col = 'red', linetype ='dashed', size = 1) +
  geom_text(x = as.Date('2019-01-01'),y = 60, label = 'Above 50 = Expansion') +
  geom_text(x = as.Date('2019-01-01'),y = 30, label = 'Below 50 = Contraction') +
  scale_x_date(date_breaks = "3 months" , date_labels = "%b-%y", limits = c(min,max)) +
  scale_y_continuous(breaks = seq(0,70,by = 10))
p1
p1 %>% ggplotly()  

I've also added a reproductible of my dataset ( pmi_data ) below

structure(list(Date = structure(c(16436, 16467, 16495, 16526, 
16556, 16587, 16617, 16648, 16679, 16709, 16740, 16770, 16801, 
16832, 16861, 16892, 16922, 16953, 16983, 17014, 17045, 17075, 
17106, 17136, 17167, 17198, 17226, 17257, 17287, 17318, 17348, 
17379, 17410, 17440, 17471, 17501, 17532, 17563, 17591, 17622, 
17652, 17683, 17713, 17744, 17775, 17805, 17836, 17866, 17897, 
17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170, 
18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444
), class = "Date"), Value = c(49.9309368121368, 58.6768073656206, 
45.7670083296354, 45.3051559795366, 39.0053846618642, 49.1441770833505, 
50.8080942369684, 51.5672278113692, 52.3474018054878, 48.684288238331, 
49.6005819277318, 41.8997241809938, 44.9937159943178, 35.9990124093785, 
43.1451562769853, 47.7227396808874, 52.890214035815, 51.5724155968251, 
53.098762758905, 48.5434650930155, 47.7228974417011, 50.4783047526896, 
45.8610689862814, 48.3232274365117, 48.528592558046, 49.3045039410955, 
51.2073193096086, 52.1261934198188, 35.2212207293696, 50.9496606483085, 
45.0422524998791, 38.0745271272649, 44.3456515676749, 46.1378825605808, 
47.6866697375406, 46.5994718651023, 43.9039427143585, 48.8655610360731, 
53.1107368639677, 46.5425509175865, 45.5076319096526, 46.8134977347412, 
46.4585946586574, 47.7383913350769, 40.2498176455257, 41.4636882678189, 
41.4500124747691, 48.0959672662869, 54.2532917453206, 48.0072394990694, 
42.6301974827825, 42.1455882001407, 47.0431096529903, 43.3389961678568, 
45.1685845453902, 55.0965589447133, 47.8335430001165, 41.0120070520458, 
45.6383412614183, 39.3644388865386, 36.6, 44.6, 33.7, 30.7, 5.1, 
43.2, 64.6)), row.names = c(NA, -67L), class = "data.frame")

The trick is to wrap the coordinates of the rects in aes() . Additionally you have to use one geom_rect . (At least when I tried with two geom_rect only one showed up). Try this:

library(plotly)

min <- as.Date("2015-01-01")
max <- as.Date("2020-07-01")

p1 <- ggplot() +
  geom_rect(aes(xmin = as.Date(c("2018-01-01", "2019-10-01")), xmax = as.Date(c("2018-07-01", "2020-07-01")),
                ymin = 0, ymax = 70), fill = "blue", alpha = 0.3, col = "grey90"
  ) +
  geom_line(data = pmi_data, aes(Date, Value), size = 1, col = "maroon") +
  labs(
    title = "Purchasing Manufacturing Index (PMI) - South Africa",
    subtitle = "Sharp Bounce in 2020 due to COVID-19",
    y = "Index Value", caption = "Data downloaded from Quantec,
                     Jan 2000 - Jul 2020 \n *Shaded areas indicate South African Recession Periods"
  ) +
  theme_solarized() +
  geom_hline(yintercept = 50, col = "red", linetype = "dashed", size = 1) +
  geom_text(x = as.Date("2019-01-01"), y = 60, label = "Above 50 = Expansion") +
  geom_text(x = as.Date("2019-01-01"), y = 30, label = "Below 50 = Contraction") +
  scale_x_date(date_breaks = "3 months", date_labels = "%b-%y", limits = c(min, max)) +
  scale_y_continuous(breaks = seq(0, 70, by = 10))

p1 %>% ggplotly()

在此处输入图像描述

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.

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