[R] Help with Vline in ggplot/ggplotly
Bill Poling
B|||@Po||ng @end|ng |rom ze||@@com
Mon Jun 17 14:55:14 CEST 2019
#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}}
More information about the R-help
mailing list