| fgts_dygraph | R Documentation |
Plots interactive time series graphs with many options for highlighting key events, regions and customizations.
fgts_dygraph( indata,
title = "", xlab="", ylab = "", roller = "default", bg_opts = "hair,both;grid,both",
splitcols = FALSE, stepcols = FALSE, hidecols = FALSE, hilightcols = FALSE,
hilightwidth = 2, hilightstyle = "solid",
events = "", event_ds = NULL,
annotations = "", annotation_ds = NULL, forecast_ds = NULL,
ylimits = NULL, dtstartfrac = 0, dtwindow = "", rebase = "", exportevents = FALSE,
meltvar = "variable", dylegend = "always", fillGraph = FALSE, colorset="lines",
groupnm = fg_sync_group(), verbose = FALSE, extraoptions = list() )
indata |
Input data in long or wide format. THere must be at least one date column, one
character column and one numeric column. Ideal format is |
title |
Title to put on top of graph |
xlab, ylab |
Labels for x and y axis |
roller |
Initial moving average value to smooth graphs. (See
|
bg_opts |
Semicomma separated options to change interactivity and background of charts. These semicomma separated options change pointer option and grids options.
|
splitcols, stepcols, hidecols |
String or list of data series to show on a second y axis, to be shown as step plots, or to be hidden.
Can also be |
hilightcols |
String or list of data series to plot in different style than other series. |
hilightwidth |
(Default: 2) relative width of series specified in |
hilightstyle |
(Default: solid). Line style of series specified in |
events |
String with possible events to add to graph. Options can be added together
with
|
event_ds |
|
annotations |
string with annotations on individual series or along
|
annotation_ds |
|
forecast_ds |
|
ylimits |
Two number vector of lower and upper limits of data to be displayed. Alternatively,
a string of the form |
dtstartfrac |
Fraction in (0,1] of dates in |
dtwindow |
String to specify date ranges applied |
rebase |
String of the form |
exportevents |
(Default: FALSE) Return list of the form |
meltvar |
(Default: |
dylegend |
(Default: "auto") Passed to |
fillGraph |
(Default: FALSE) Shade area underneath each series. |
colorset |
(Default: "lines") Set of default colors to use. See Customization vignette . |
groupnm |
(Default: NULL, unless set via |
verbose |
(Default: FALSE) Print extra details about what will be graphed. |
extraoptions |
Additional options passed to |
Input data can either be in wide ('date' ,'series1',...) format or normalized (long) format
('date','variable','value') format. This package infers date columns names from column types and seeks to be as agnostic
as possible as to column names.
Colors can be managed using fg_update_aes() and will persist across R sessions, See vignette for details.
Series are grouped together into bands around a series series if their names end as in 'series.lo' or 'series.hi'. See examples and vignette for details.
Events are dates and date ranges to be highlighted in the graph. Multiple types of events can be strung together in semicolon delimited strings. Of the options outlined above, two additional details are
"doi,<category>" gets events from fg_get_dates_of_interest() which can be added to or managed using fg_update_dates_of_interest().
Colors and label placement can be customized as necessary.
seasonal,<type> puts regularly occurring events on the graph. <type> can be
<type> | description |
"optex,mo | qtr" | Monthly and/or quarterly equity option expiration dates. |
"roll" | IMM CDS roll dates |
"daysfromroll" | Dates with same number of days to the next roll as last date plotted |
"doq","doy","bdoy" | Dates with same day in quarter, in year, or business day of year to the last day plotted |
Events can also be added using a data.frame passed via event_ds with the following columns:
| column | description | type |
date | (Required) Start date | Date |
date_end | End date to specify range of a colored band | Date |
text | (Required) Text to display | character |
color | Color for line and text | character |
eventonly | Only draw line for for start of event, no band | logical |
strokePattern | One of ('solid','dashed' (Default) ,'dotted','dotdash') | character |
loc | one of ('top','bottom' (Default)) | character |
series | Name of series to apply event to, if needed | character |
category | Optional string used for exceptions. See notes below. | character |
Many times, events depend on outside data or statistical analysis on the original data. The event_ds to be passed
in can come from event helpers in fg_cut_to_events(), fg_addbreakouts(), fg_findTurningPoints(), or fg_ratingsEvents().
Event columns are processed as is, unless category=="series_color" which will replace color with that of its series.
Annotiations include any notes or highlights added to the graph on the 'y' axis or on an individual series. In addition to those passed
via the annotations parameter, annotations can be added using a data.frame with the following columns:
| column | description | type |
date | (Required) Start date | Date |
date_end | End date to specify range of a colored band | Date |
text | (Required) Text to display | character |
color | Color for line and text | character |
eventonly | Only draw line for for start of event, no band | logical |
Other notes:
Using stepcols most often happens with lower frequency data, so an nafill is automatically performed.
Dates in event types pt and dt are adjusted to next day in series if they do not already exist.
Dygraph dygraphs plotting input data, with annotations and other customizations.
# See Vignette for more extensive examples.
# Basic Example
fgts_dygraph(eqtypx, title="Stock Prices", ylab="Adjusted Close")
# With series Highlights, finer resolution and focused date range
fgts_dygraph(eqtypx, dtstartfrac=0.8,hilightcols="IBM",hilightwidth=4,roller=3)
# Rebasing to 1/1/2022
fgts_dygraph(eqtypx, title="Rebased Prices", ylab="Adjusted Close",rebase="2022-01-01")
# Using bands (.lo, .hi)
toplot <- reerdta[REGION=="LATAM",.(cop=sum(value*(variable=="COL")),
reer=mean(value),reer.lo=min(value),reer.hi=max(value)),by=.(date)]
fgts_dygraph(toplot,title="COP REER vs Latam peers",roller=3,hilightcols="cop",hilightwidth=4)
# Events Examples. Notice how roller shortens with the series.
# See Vignette for more extensive examples
require(data.table)
smalldta <- narrowbydtstr(eqtypx[,.(date,TLT,EEM)],"-3y::")
fgts_dygraph(smalldta,events="doi,regm;doi,fedmoves")
fgts_dygraph(smalldta,events="date,FOMO,2025-01-01,2025-06-01;date,xmas,2025-12-25")
# Events passed in as data.frames
myevents = data.frame(end_date=as.Date(c("2024-03-10","2024-06-10")),
date=as.Date(c("2024-01-10","2024-04-10")),
text=c("range","event"),color=c("green","red"))
fgts_dygraph(smalldta,events="doi,fedmoves",event_ds=myevents)
# Annotations on y axis
fgts_dygraph(eqtypx,annotations="last,linevalue")
fgts_dygraph(eqtypx,annotations="hline,100,at100,red;hline,200,at200;range,300,400")
# use with helpers
smalldta <- narrowbydtstr(eqtypx[,.(date,IBM,QQQ)],"-2y::")
fgts_dygraph(smalldta,title="W TurnPts",event_ds=fg_findTurningPoints(smalldta[,.(date,QQQ)]))
fgts_dygraph(smalldta,title="W Sentiment",event_ds=fg_cut_to_events(consumer_sent,center="zscore"))
fgts_dygraph(smalldta,title="W dividends",event_ds=fg_tq_divs(c("IBM","QQQ")))
# Other helpers for use with credit ratings, breakouts, and earnings data are available.
# use with forecasts
require(forecast)
require(timetk)
require(sweep)
smalldta <- narrowbydtstr(eqtypx[,.(date,IBM,QQQ)],"-2y::")
fcst_one <- function(ticker) {
fcst <-tk_ts(smalldta[,.SD,.SDcol=c("date",ticker)]) |> ets() |> forecast::forecast(h=30)
fcst |> sweep::sw_sweep(timetk_idx=TRUE) |> fg_sweep()
}
fpred <- merge(fcst_one("QQQ"),fcst_one("IBM"),by="date")
fgts_dygraph(smalldta,title="With Forecasts", dtstartfrac=0.7,rebase=",100",forecast_ds=fpred)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.