Skip to content

Commit

Permalink
Precommit not working.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereckmezquita committed Jul 6, 2024
1 parent 3a4096d commit fba0e7a
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 2,285 deletions.
3 changes: 0 additions & 3 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.graphics
^.*\.Rproj$
^\.Rproj\.user$
^\.pre-commit-config\.yaml$
^renv$
^renv\.lock$
demo/
dev/
1 change: 0 additions & 1 deletion .Rprofile

This file was deleted.

85 changes: 0 additions & 85 deletions .pre-commit-config.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ License: All rights reserved.
Encoding: UTF-8
LazyData: true
VignetteBuilder: knitr
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Suggests:
knitr,
rmarkdown,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export(stat_shade_ranges)
export(theme_dereck_dark)
export(theme_dereck_light)
import(data.table)
import(ggplot2)
importFrom(ggplot2,'%+replace%')
8 changes: 0 additions & 8 deletions R/ddplot.R

This file was deleted.

3 changes: 3 additions & 0 deletions R/dmplot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#' @import ggplot2
#' @import data.table
NULL
38 changes: 17 additions & 21 deletions R/stat_movingaverages.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# df <- data.table::fread("data/kucoin_prices.csv")

StatMovingAverage <- ggplot2::ggproto(
Expand Down Expand Up @@ -32,62 +31,62 @@ StatMovingAverage <- ggplot2::ggproto(

#' @title Moving averages `ggplot2` layer
#' @author Dereck Mezquita
#'
#' @description
#'
#' @description
#' `stat_movingaverages` is a `ggplot2` layer that allows you to plot moving averages on a `ggplot2` plot either by providing the column names `ggplot2::aes` of the previously calculated metrics.
#'
#'
#' You are free to use whatever algorithm you desire; the result will be two line plots one for a short moving average and one for a long moving average.
#'
#'
#' To use this layer provide `ggplot2::aes` values for `x` (datetime x-axis) and `short` and `long` (y-axis).
#'
#'
#' See examples for more details.
#'
#' @param mapping A `ggplot2::aes` object (required - default `NULL`).
#' @param data A `data.table` object (required - default `NULL`).
#' @param linewidth A `numeric` vector of length one; the width of the line (optional - default `1.75`).
#' @param alpha A `numeric` vector of length one; the alpha of the line (optional - default `0.75`).
#' @param colour A named or unnamed `list` with three elements "short" and "long". These are the colours for the short and long moving averages (optional - default `list(short = "red", long = "blue")`).
#'
#'
#' @section Aesthetics:
#' \code{stat_movingaverages} understands the following aesthetics (required aesthetics are in bold):
#' \itemize{
#' \item \strong{x} -- datetime (x-axis)
#' \item \strong{short} -- the values for the short moving average (y-axis).
#' \item \strong{long} -- the values for the long moving average (y-axis)
#' }
#'
#'
#' @details
#'
#'
#' This is a `ggplot2` extension; it is used with the `+` operator for adding a layer to a `ggplot2` object.
#'
#' @return A `ggplot2::layer` object.
#'
#' @examples
#'
#'
#' # get some financial data
#' # kucoin is private package - you can use any data source
#' ticker <- "BTC/USDT"
#'
#'
#' dt <- kucoin::get_market_data(
#' symbols = ticker,
#' from = "2022-11-28 15:29:43 EST", # lubridate::now() - lubridate::days(7),
#' to = "2022-12-05 15:29:31 EST",# lubridate::now(),
#' to = "2022-12-05 15:29:31 EST", # lubridate::now(),
#' frequency = "1 hour"
#' )
#'
#'
#' dt
#'
#'
#' # we need a function that calculates the indicator for us
#' # typically I like to write my own functions in C++; in this case we will use TTR's
#' # the stat expects a named list to be returned - we redefine ttr
#' ema <- function(close, n = 2, wilder = TRUE) {
#' return(as.list(as.data.frame(TTR::EMA(close, n = n, wilder = wilder))))
#' }
#'
#'
#' # calculate the short and long moving averages
#' dt[, ema_short := ema(close, n = 10, wilder = TRUE)]
#' dt[, ema_long := ema(close, n = 50, wilder = TRUE)]
#'
#'
#' dt |>
#' ggplot2::ggplot(ggplot2::aes(
#' x = datetime,
Expand Down Expand Up @@ -117,7 +116,7 @@ StatMovingAverage <- ggplot2::ggproto(
#' axis.text.x = ggplot2::element_text(angle = 75, vjust = 0.925, hjust = 0.975),
#' panel.grid.minor = ggplot2::element_blank()
#' )
#'
#'
#' @export

stat_movingaverages <- function(
Expand All @@ -134,8 +133,7 @@ stat_movingaverages <- function(
short = "yellow",
long = "purple"
),
...
) {
...) {
list(
ggplot2::layer(
stat = StatMovingAverage,
Expand Down Expand Up @@ -187,5 +185,3 @@ stat_movingaverages <- function(
# # allows user to choose which values to use for calculations
# # accepts wilder as an additional argument
# stat_movingaverages(ggplot2::aes(y = close), FUN = TTR::SMA)


70 changes: 46 additions & 24 deletions R/stat_shade_ranges.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

#' StatShadedDateRange
#'
#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
StatShadedDateRange <- ggplot2::ggproto(
"StatShadedDateRange",
ggplot2::Stat,
Expand Down Expand Up @@ -66,32 +71,50 @@ StatShadedDateRange <- ggplot2::ggproto(
)


#' @title Shaded Date Ranges `ggplot2` layer
#' @author Dereck Mezquita
#' Shaded Date Ranges ggplot2 layer
#'
#' @param mapping A `ggplot2::aes` object (required - default `NULL`).
#' @param data A `data.table` object (required - default `NULL`).
#' @param ... Additional arguments passed to `ggplot2::layer`.
#' This function creates a ggplot2 layer that shades date ranges based on grouped data.
#' It's particularly useful for highlighting specific periods in time series data.
#'
#' @details
#' @param mapping Set of aesthetic mappings created by \code{\link[ggplot2]{aes}}.
#' Must include `x` for dates and `group` for categorizing ranges.
#' @param data The data to be displayed in this layer. If NULL, the default,
#' the data is inherited from the plot data as specified in the call to \code{\link[ggplot2]{ggplot}}.
#' @param geom The geometric object to use display the data. Default is "linerange".
#' @param position Position adjustment, either as a string, or the result of a call to a position adjustment function. Default is "identity".
#' @param na.rm If FALSE, the default, missing values are removed with a warning.
#' If TRUE, missing values are silently removed.
#' @param show.legend logical. Should this layer be included in the legends?
#' NA, the default, includes if any aesthetics are mapped.
#' @param inherit.aes If FALSE, overrides the default aesthetics,
#' rather than combining with them.
#' @param ... Other arguments passed on to \code{\link[ggplot2]{layer}}.
#' These are often aesthetics, used to set an aesthetic to a fixed value.
#'
#' This is a `ggplot2` extension; it is used with the `+` operator for adding a layer to a `ggplot2` object.
#'
#' You can set the colour by fill aesthetic with a column.
#' @return A ggplot2 layer object.
#'
#' @return A `ggplot2::layer` object.
#'
#' @examples
#' \dontrun{
#' dt |>
#' ggplot2::ggplot(ggplot2::aes(x = datetime)) +
#' ggplot2::geom_line(ggplot2::aes(y = close), linewidth = 1.5, colour = "white") +
#' plots$stat_shade_ranges(ggplot2::aes(group = call, fill = call), alpha = 0.25) +
#' ggplot2::scale_color_manual(
#' name = "call",
#' values = c("buy" = "green", "sell" = "red", "none" = "gray"),
#' aesthetics = "fill"
#' )
#' library(ggplot2)
#' library(data.table)
#'
#' # Create sample data
#' dt <- data.table(
#' datetime = seq(as.POSIXct("2023-01-01"), as.POSIXct("2023-12-31"), by = "day"),
#' close = runif(365, 100, 200),
#' call = sample(c("buy", "sell", "none"), 365, replace = TRUE, prob = c(0.3, 0.3, 0.4))
#' )
#'
#' # Create plot with shaded ranges
#' ggplot(dt, aes(x = datetime)) +
#' geom_line(aes(y = close), linewidth = 1.5, colour = "white") +
#' stat_shade_ranges(aes(group = call, fill = call), alpha = 0.25) +
#' scale_fill_manual(
#' name = "Call",
#' values = c("buy" = "green", "sell" = "red", "none" = "gray")
#' ) +
#' theme_minimal() +
#' labs(title = "Stock Price with Buy/Sell Signals", x = "Date", y = "Price")
#' }
#'
#' @export
Expand All @@ -103,8 +126,7 @@ stat_shade_ranges <- function(
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
...
) {
...) {
list(
ggplot2::layer(
stat = StatShadedDateRange,
Expand All @@ -120,4 +142,4 @@ stat_shade_ranges <- function(
)
)
)
}
}
15 changes: 4 additions & 11 deletions R/theme_dereck.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' ggplot2::ggplot(ggplot2::aes(x = Sepal.Length, y = Sepal.Width)) +
#' ggplot2::geom_point(ggplot2::aes(color = Species, shape = Species)) +
#' ggplot2::labs(title = "Iris data", subtitle = "Classic data", x = "Sepal Length", y = "Sepal Width") +
#' ggplot2::facet_grid(~ Species) +
#' ggplot2::facet_grid(~Species) +
#' dmplot::theme_dereck_light()
#'
#' data.frame(year = time(nhtemp), tmps = as.matrix(nhtemp)) |>
Expand All @@ -24,20 +24,16 @@ theme_dereck_light <- function(base_size = 11, base_family = "") {
ggplot2::theme_bw(base_size = base_size, base_family = base_family) %+replace% ggplot2::theme(
plot.title = ggplot2::element_text(face = "bold", hjust = 1, margin = ggplot2::margin(0, 0, 5, 0, unit = "pt")), # 0.5 centres when position is set to "plot"
plot.subtitle = ggplot2::element_text(hjust = 1, vjust = 0.015, margin = ggplot2::margin(0, 0, 5, 0, unit = "pt")),

panel.background = ggplot2::element_blank(),
# panel.border = ggplot2::element_rect(color = "#afafaf", fill = NA),
axis.line = ggplot2::element_blank(),
panel.border = ggplot2::element_blank(),

panel.grid.minor = ggplot2::element_line(color = "#454545", size = 0.135, linetype = "dotted"),
panel.grid.major = ggplot2::element_line(color = "#aaaaaa", size = 0.175, linetype = "longdash"),

# axis.line = ggplot2::element_line(color = "#afafaf"),
axis.ticks = ggplot2::element_line(color = "#aaaaaa"),

strip.background = ggplot2::element_rect(fill = "white", colour = "#676767"),

legend.position = "bottom",
legend.title = ggplot2::element_text(vjust = 0.85) # adjust title position higher to key when bottom
)
Expand All @@ -55,7 +51,7 @@ theme_dereck_light <- function(base_size = 11, base_family = "") {
#' ggplot2::ggplot(ggplot2::aes(x = Sepal.Length, y = Sepal.Width)) +
#' ggplot2::geom_point(ggplot2::aes(color = Species, shape = Species)) +
#' ggplot2::labs(title = "Iris data", subtitle = "Classic data", x = "Sepal Length", y = "Sepal Width") +
#' ggplot2::facet_grid(~ Species) +
#' ggplot2::facet_grid(~Species) +
#' dmplot::theme_dereck_dark()
#'
#' data.frame(year = time(nhtemp), tmps = as.matrix(nhtemp)) |>
Expand All @@ -74,23 +70,20 @@ theme_dereck_dark <- function(base_size = 12, base_family = "") {
# panel.border = ggplot2::element_rect(color = "#afafaf", fill = NA),
axis.line = ggplot2::element_blank(),
panel.border = ggplot2::element_blank(),

panel.grid.minor = ggplot2::element_line(color = "white", size = 0.135, linetype = "dotted"),
panel.grid.major = ggplot2::element_line(color = "#aaaaaa", size = 0.175, linetype = "longdash"),

# axis.line = ggplot2::element_line(color = "#afafaf"),
axis.ticks = ggplot2::element_line(color = "#aaaaaa"),

strip.background = ggplot2::element_rect(fill = "white", colour = "#676767"),

legend.position = "bottom",
legend.title = ggplot2::element_text(vjust = 0.85), # adjust title position higher to key when bottom

# dark theme configs
plot.background = ggplot2::element_rect(fill = "#333333"),
panel.background = ggplot2::element_rect(fill = "#1E1E1E"),
legend.background = ggplot2::element_rect(fill = "#252526"), #
legend.background = ggplot2::element_rect(fill = "#252526"), #
text = ggplot2::element_text(colour = "#CFCFCF"),
axis.text = ggplot2::element_text(colour = "#CFCFCF")
)
}
}
Loading

0 comments on commit fba0e7a

Please sign in to comment.