1

I'm designing an flexdashboard output using ggplotly with geom_vline. With the next code, I can view the plot as I need:

library(openair)
library(tidyverse)
library(plotly)

aq <- importAURN(site = c("my1", "nott"), year = 2000,
                 pollutant = c("nox", "no2", "o3"))
aq <- aq %>% pivot_longer(cols = 2:4, names_to = "Parameter", values_to = "Value")

event <- tibble(date = c("2000-03-23 11:00:00","2000-05-29 11:40:00","2000-08-22 12:00:00"))
event$date <- as.POSIXct(event$date,format="%Y-%m-%d %H:%M", tz = "UTC")

b <- ggplot(aq,
       aes(x = date, 
           y = Value, 
           colour = site,
           group = site)) + 
  geom_line() + 
  geom_vline(data = event ,
             aes(xintercept = as.numeric(date)),
             linetype = 2, 
             colour = "blue",
             show.legend = TRUE) +
  theme_bw() +
  facet_grid(Parameter ~ .,
             scales = "free")

ggplotly(b,dynamicTicks = TRUE) %>% 
  layout(legend = list(orientation = "h", y = -0.2))

First flexdashboard output OK

But, when I use the same code structure, in other flexdashboard output, the plot don't show the vertical lines:

fecha.evento <- tibble(date = as.character(evento.q$`Toma conocimiento`))
fecha.evento$date <- as.POSIXct(fecha.evento$date,format="%Y-%m-%d %H:%M:%S", tz = "UTC")

a <- ggplot(quintero,
       aes(x = date,
           y = dato_val_ug,
           colour = Ubicación, 
           group = Ubicación)) + 
  geom_line() + 
  geom_vline(data = fecha.evento,
             aes(xintercept = as.numeric(date)),
             linetype = 2, 
             colour = "blue",
             show.legend = TRUE) +
  theme_bw() +
  facet_grid(Parámetro ~ .,
             scales = "free")

ggplotly(a,dynamicTicks = TRUE) %>% 
  layout(legend = list(orientation = "h", y = -0.2))

Flexdashboard output with problem using geom_vline

I think the problem is with de event dates. In the first case I created an tibble based on dates created manually: event. In the second case, I use the data upload from excel and adapted to the same structure in event:

> str(event)
tibble [3 × 1] (S3: tbl_df/tbl/data.frame)
 $ date: POSIXct[1:3], format: "2023-03-23 11:00:00" "2023-05-02 11:40:00" "2023-04-22 12:00:00"

> str(fecha.evento)
tibble [10 × 1] (S3: tbl_df/tbl/data.frame)
 $ date: POSIXct[1:10], format: "2023-03-23 11:00:00" "2023-03-29 11:40:00" "2023-03-30 12:00:00" "2023-03-31 10:30:00" ...

I have tried to modify the type of variable: character or date, but it does not work.

I need some guidance on what could be happening so I can fix it.

I reviewed this post: ggplotly fails with geom_vline() with xintercept Date value, and change my code, but don't work.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.