Skip to content

Commit

Permalink
Clean up plot_lines() in addPolygon()
Browse files Browse the repository at this point in the history
This makes the function consistent with the similar function in
addSeries(). 'xDataSubset' prevents unnecessary repeated subsetting.
The 'xsubset' if statement handles some special cases. Merge with an
empty xts object to create 'ta.y' to avoid allocating the coredata.
  • Loading branch information
joshuaulrich committed Jul 24, 2023
1 parent 48cadd6 commit 05dd10c
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -1023,20 +1023,27 @@ addPolygon <- function(x, y=NULL, main="", on=NA, col=NULL, ...){
lenv$plot_lines <- function(x, ta, on, col, ...){
xdata <- x$Env$xdata
xsubset <- x$Env$xsubset
xDataSubset <- xdata[xsubset]
if(is.null(col)) col <- x$Env$theme$col
if(all(is.na(on))){
# Add x-axis grid lines
x$Env$x_grid_lines(xdata[xsubset], x$Env$grid.ticks.on, par("usr")[3:4])
x$Env$x_grid_lines(xDataSubset, x$Env$grid.ticks.on, par("usr")[3:4])
}
# we can add points that are not necessarily at the points
# on the main series
subset.range <- paste(start(xdata[xsubset]),
end(xdata[xsubset]),sep="/")
ta.adj <- merge(n=.xts(1:NROW(xdata[xsubset]),
.index(xdata[xsubset]),
tzone=tzone(xdata)),ta)[subset.range]
# on the main series, but need to ensure the new series only
# has index values within the xdata subset
if(xsubset == "") {
subset.range <- xsubset
} else {
fmt <- "%Y-%m-%d %H:%M:%OS6"
subset.range <- paste(format(start(xDataSubset), fmt),
format(end(xDataSubset), fmt), sep = "/")
}

xds <- .xts(, .index(xDataSubset), tzone=tzone(xdata))
ta.y <- merge(ta, xds)[subset.range]
# NAs in the coordinates break the polygon which is not the behavior we want
ta.y <- na.omit(ta.adj[,-1])
ta.y <- na.omit(ta.y)

# x coordinates
n <- seq_len(NROW(ta.y))
Expand Down

0 comments on commit 05dd10c

Please sign in to comment.