[R] Help with Vline in ggplot/ggplotly

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Mon Jun 17 15:16:35 CEST 2019


Hi

I may be completely missing your point but maybe simple

geom_vline(xintercept = as.POSIXct("2019-06-11"))

is what you want.

Something like

ggplot(aes(Date2,NetEditRev)) +
  geom_line(alpha=0.5, color = "#2c3e50") +
  geom_smooth(method = "loess", span= 0.5) +
  theme_tq() +
  scale_y_continuous(labels = scales::dollar_format()) +
geom_vline(xintercept = as.POSIXct("2019-06-11"))

Cheers
Petr

> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Bill Poling
> Sent: Monday, June 17, 2019 2:55 PM
> To: r-help (r-help using r-project.org) <r-help using r-project.org>
> Subject: [R] Help with Vline in ggplot/ggplotly
>
> #RStudio Version 1.2.1335
> sessionInfo()
> #R version 3.5.3 (2019-03-11)
> #Platform: x86_64-w64-mingw32/x64 (64-bit) #Running under: Windows >= 8
> x64 (build 9200)
>
> Good morning.
>
> I am trying to add a Vline to a ggplot which will ultimately become ggplotly
> version
>
> My data span 2017 - 2019-06-11 with an additional 92 days of estimates = 984
> days
>
> The estimates begin at 2019-06-12 through 2019-09-11 so I want to add vline
> at 2019-06-11
>
> Here are some data particulars:
>
> summary(ednet)
> #Date2              NetEditRev
> # Min.   :2017-01-01   Min.   :-176182.7500
> # 1st Qu.:2017-09-03   1st Qu.:   2584.1825
> # Median :2018-05-07   Median :  49671.7200
> # Mean   :2018-05-07   Mean   :  46078.4093
> # 3rd Qu.:2019-01-08   3rd Qu.:  77373.0000
> # Max.   :2019-09-11   Max.   : 164891.2500
>
> as_tibble(ednet)
> # A tibble: 984 x 2
> # Date2      NetEditRev
> # <date>          <dbl>
> # 1 2017-01-01     -923.
> # 2 2017-01-02    19222.
> # 3 2017-01-03    -8397.
> # 4 2017-01-04    37697.
> # 5 2017-01-05    46075.
> # 6 2017-01-06    38329.
> # 7 2017-01-07     3111.
> # 8 2017-01-08       21.4
> # 9 2017-01-09    63570.
> # 10 2017-01-10    50777
> # ... with 974 more rows
>
> head(ednet)
>         Date2 NetEditRev
> 1: 2017-01-01    -923.40
> 2: 2017-01-02   19222.09
> 3: 2017-01-03   -8396.82
> 4: 2017-01-04   37696.58
> 5: 2017-01-05   46075.34
> 6: 2017-01-06   38329.35
>
>
> #The plot I have currently works fine
> g1 <- as_tibble(ednet) %>%
>   ggplot(aes(Date2,NetEditRev)) +
>   geom_line(alpha=0.5, color = "#2c3e50") +
>   geom_smooth(method = "loess", span= 0.5) +
>   theme_tq() +
>   scale_y_continuous(labels = scales::dollar_format()) +
>   labs(title="Plot1 for Net Edit Revenue True through 2019-06-11 with
> Estimates Beyond",
>      x = "",
>      y = "Revenue"
> )
> ggplotly(g1) %>%
>   layout(xaxis = list(rangeslider = list(type = "Date")))
>
>
>
>
> But I need to add a Vline at 2019-06-11
> So I looked at many URLs on the subject and tried different suggestions,
> however, none seem to work
>
> #https://github.com/tidyverse/ggplot2/issues/84
> #https://ggplot2.tidyverse.org/reference/geom_abline.html
>
> Then I found this link and thought it might work for me so I am trying it
> #https://stackoverflow.com/questions/52735018/ggplotly-does-not-display-
> geom-vline-geom-hline-when-data-is-posixct
>
> # Need an event DF for the vline
>
> event = data.frame(event_date = as.POSIXct(c("2019-06-11")))
>
>
> I have tried it various ways and as a tibble or as just a DF (not sure that makes
> any difference?)
>
> I think I am close and I hope someone can add the missing piece to this please.
>
> Thank you
>
> WHP
>
> V1  --- NOT tibble   with   geom_vline(data = event, aes(xintercept =
> event$event_date), color = "red") +
>
> g1 <- #as_tibble(ednet) %>%
>       ednet %>%
>   ggplot(aes(Date2,NetEditRev)) +
>   geom_line(alpha=0.5, color = "#2c3e50") +
>   geom_smooth(method = "loess", span= 0.5) +
>   theme_tq() +
>   scale_y_continuous(labels = scales::dollar_format()) +
>   #geom_vline(xintercept = ) +
>   #geom_vline(aes(xintercept = as.integer(as.POSIXct("2019-06-11"))), col =
> "black") +
>   #geom_vline(data = ednet, aes(xintercept = as.numeric("2019-06-11"))) +
>   #geom_vline(data = ednet, aes(xintercept = as.numeric(Date2="2019-06-
> 11"))) +
>   #geom_vline(data = event, aes(xintercept = as.numeric(event$event_date)),
> color = "red") +
>   geom_vline(data = event, aes(xintercept = event$event_date), color = "red") +
>   labs(title="Plot1 for Net Edit Revenue True through 2019-06-11 with
> Estimates Beyond",
>   x = "",
>   y = "Revenue"
> )
>
> ggplotly(g1) %>%
>   layout(xaxis = list(rangeslider = list(type = "Date")))
>
>
>
> V2  --- As tibble   with   geom_vline(data = event, aes(xintercept =
> event$event_date), color = "red") +
>
> g1 <- as_tibble(ednet) %>%
>   ggplot(aes(Date2,NetEditRev)) +
>   geom_line(alpha=0.5, color = "#2c3e50") +
>   geom_smooth(method = "loess", span= 0.5) +
>   theme_tq() +
>   scale_y_continuous(labels = scales::dollar_format()) +
>   #geom_vline(xintercept = ) +
>   #geom_vline(aes(xintercept = as.integer(as.POSIXct("2019-06-11"))), col =
> "black") +
>   #geom_vline(data = ednet, aes(xintercept = as.numeric("2019-06-11"))) +
>   #geom_vline(data = ednet, aes(xintercept = as.numeric(Date2="2019-06-
> 11"))) +
>   #geom_vline(data = event, aes(xintercept = as.numeric(event$event_date)),
> color = "red") +
>   geom_vline(data = event, aes(xintercept = event$event_date), color = "red") +
>   labs(title="Plot1 for Net Edit Revenue True through 2019-06-11 with
> Estimates Beyond",
>      x = "",
>      y = "Revenue"
> )
> ggplotly(g1) %>%
>   layout(xaxis = list(rangeslider = list(type = "Date")))
>
>
> V3  --- Not as Tibble geom_vline(data = event, aes(xintercept =
> as.numeric(event$event_date)), color = "red") +
>
> g1 <- ednet %>%
>   ggplot(aes(Date2,NetEditRev)) +
>   geom_line(alpha=0.5, color = "#2c3e50") +
>   geom_smooth(method = "loess", span= 0.5) +
>   theme_tq() +
>   scale_y_continuous(labels = scales::dollar_format()) +
>   #geom_vline(xintercept = ) +
>   #geom_vline(aes(xintercept = as.integer(as.POSIXct("2019-06-11"))), col =
> "black") +
>   #geom_vline(data = ednet, aes(xintercept = as.numeric("2019-06-11")))
>   #geom_vline(data = ednet, aes(xintercept = as.numeric(Date2="2019-06-
> 11")))
>   geom_vline(data = event, aes(xintercept = as.numeric(event$event_date)),
> color = "red") +
>   #geom_vline(data = event, aes(xintercept = event$event_date), color = "red")
> +
>   labs(title="Plot1 for Net Edit Revenue True through 2019-06-11 with
> Estimates Beyond",
>        x = "",
>        y = "Revenue"
>   )
> ggplotly(g1) %>%
>   layout(xaxis = list(rangeslider = list(type = "Date")))#Fix Layout to layout, fix
> List to list
>
>
> V4  --- as Tibble geom_vline(data = event, aes(xintercept =
> as.numeric(event$event_date)), color = "red") +
>
> g1 <- as_tibble(ednet) %>%
>   ggplot(aes(Date2,NetEditRev)) +
>   geom_line(alpha=0.5, color = "#2c3e50") +
>   geom_smooth(method = "loess", span= 0.5) +
>   theme_tq() +
>   scale_y_continuous(labels = scales::dollar_format()) +
>   #geom_vline(xintercept = ) +
>   #geom_vline(aes(xintercept = as.integer(as.POSIXct("2019-06-11"))), col =
> "black") +
>   #geom_vline(data = ednet, aes(xintercept = as.numeric("2019-06-11")))
>   #geom_vline(data = ednet, aes(xintercept = as.numeric(Date2="2019-06-
> 11")))
>   geom_vline(data = event, aes(xintercept = as.numeric(event$event_date)),
> color = "red") +
>   #geom_vline(data = event, aes(xintercept = event$event_date), color = "red")
> +
>   labs(title="Plot1 for Net Edit Revenue True through 2019-06-11 with
> Estimates Beyond",
>        x = "",
>        y = "Revenue"
>   )
>
> ggplotly(g1) %>%
>   layout(xaxis = list(rangeslider = list(type = "Date")))
>
> Confidentiality Notice This message is sent from Zelis. ...{{dropped:13}}
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/



More information about the R-help mailing list