| fg_scatplot | R Documentation |
Plots bivariate plots with some time-series specific enhancements. Rather than programmatically describing graph aesthetics, a simple formula-based approach is used. This approach allows quick specification of many customizaton options.
fg_scatplot(indata,plotform,type="scatter",datecuts=c(7,66),
noscales="",xdecoration="",ydecoration="",annotatecorners="",
tsize=3,psize=1,n_color_switch=7,n_hex_switch=400,repel=TRUE,jitter=c(0,0),
title="",subtitle="",caption="",axislabels="",
boundbox=c(),boundboxtype="",gridstyle=NA_character_,legendinside=FALSE,
tformula=formula("y~x"),returnregresults=FALSE,
keepcols="", meltvar="variable",melted=NULL)
indata |
|
plotform |
A text formula describing how to set up the graph. The formula is of the general form
where Aesthetic options:
Date specific options:
Text options:
Other annotations:
|
type |
character string for the type of graph to plot:
if |
datecuts |
list of integers (Default |
noscales |
String to suppress guides with any of |
xdecoration, ydecoration |
2 element string list to add to either side of an axis label. |
annotatecorners |
4 element string list to add notes to each of 4 quadrants of the graph. See examples. |
tsize |
default text size (with some scaled variations for graph parts such as titles) |
psize |
default point size. |
n_color_switch |
(Default 7): Number of distint color categories beyond which colors are taken from gradient scales,
unless a color set is specified in a |
n_hex_switch |
(Default 400): Number of data points beyond which points are replaced with binned hexagons (see |
repel |
(Default TRUE) Text and labels are plotted using |
jitter |
Jitter parameters used by |
title |
Title to add to graph |
subtitle |
Subtitle to add to graph |
caption |
Caption to add to graph |
axislabels |
Semicolon separated string with x and y labels, e.g |
boundboxtype, boundbox |
string describing how to use bounding boxes. If
|
gridstyle |
String in |
legendinside |
(Default: TRUE) Put all guides inside the graph. |
tformula |
(Default |
returnregresults |
Return a two element list c(plot,regression |
keepcols |
list of |
meltvar |
(Default |
melted |
(Default:NULL) If |
indata 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 casts or
pivot_wider to get x and y columns. Note that aesthetic characteristics (if used) must be present for both
long and wide input formats.
Default aesehetic sets used for portions of the graph each have their own names, which can be seeing by running
fg_print_aes_list() and modified or added to using fg_update_aes(). The default theme can be modified using
fg_replace_theme(). Both aesthetic changes and theme changes are persistent across R sessions.
Use of doi: in plotform string supercedes color aesthetics otherwise specified. (Is this true?)
If tooltips are used, the result of fg_scatplot must be viewed using print(girafe(ggobj=fg_scatplot(...)))
See ggiraph::geom_point_interactive()
Winsorized hulls with quantile cutoff q are formed using the closest (by euclidean distance) 1-q points to the
geograpic center of the entire set.
Captions are added if data is truncated or omitted by the bounding box procedure.
A ggplot2::ggplot() object with desired graph, or a ggiraph::girafe() object if tooltips is in the plotform string.
# Simple text examples
require(data.table)
dt_mtcars=data.table(datasets::mtcars)
dt_mtcars$id=lapply(rownames(datasets::mtcars),\(x) last(strsplit(x," ")[[1]]))
fg_scatplot(dt_mtcars,"disp ~ hp + color:am + text:id","scatter",title="text basic")
fg_scatplot(dt_mtcars,"disp ~ hp + color:carb + label:id","scatter",
n_color_switch=0,title="scat color switch")
# Plotting data with dates:
set.seed(1); ndates <- 400; dlyvol <- 0.2/sqrt(260)
rtns <- cbind(cumsum(rnorm(ndates,sd=dlyvol)),cumsum(rnorm(ndates,sd=dlyvol)))
dttest <- data.table(date=seq(as.Date("2021-01-01"),as.Date("2021-01-01")+ndates-1),
xtest=100*(1+rtns[,1]),ytest=100*(1+rtns[,2]),
ccat=fifelse(runif(ndates)<=0.2,"Rare","mkt"))
# Making categories out of recent data
fg_scatplot(dttest,"ytest ~ xtest + doi:recent","scatter",datecuts=c(66,122),title="from recent")
fg_scatplot(dttest ,"ytest ~ xtest + color:ccat + doi:recent + point:label","scat",
datecuts=c(7,66),title="recent w label")
# Makes categories out of event sets from [fg_get_dates_of_interest()]
fg_scatplot(dttest,"ytest ~ xtest + doi:regm","scatter",title="from a regime")
# Point graphing switches.
fg_scatplot(dttest,"ytest ~ xtest + color:ccat","lm",n_hex_switch=100,title="Hex Switch")
# Quick changes to aesthetic sets
fg_scatplot(dttest,"ytest ~ xtest + color:ccat,altlines_6","loess",title="Alternate colors")
# Extra summarizatons
fg_scatplot(dttest,"ytest ~ xtest + color:ccat + hull:0.1 + ellipse","lm",title="Curves")
# Annotations
fg_scatplot(dttest,"ytest ~ xtest + color:ccat + point:labelall","scat",title="Last Values")
fg_scatplot(dttest,"ytest ~ xtest + color:ccat + point:anno","scat",annotatecorners="NW;NE;SE;SW",
legendinside = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.