Plotting OHLC bars with TimeSeries.jl and GR.jl - julia

I am using the following taken from the examples https://juliastats.org/TimeSeries.jl/latest/plotting/
using Plots
using TimeSeries
gr()
ta = [[1,5,0,2]]
plot(ta, seriestype = :candlestick)
But I'm getting the error
ERROR: LoadError: The backend must not support the series type Val{:candlestick}, and there isn't a series recipe defined.

It has to be a TimeArray
data = (datetime = [DateTime(2018, 11, 21, 12, 0), DateTime(2018, 11, 21, 13, 0)],
open = [1, 11.2],
high = [5, 21.2],
low = [0, 31.2],
close = [2, 31.2])
ta = TimeArray(data; timestamp = :datetime, meta = "Example")

Related

r circlize: missing value where TRUE/FALSE needed

I am trying to plot (for the first time) a chord diagram in the package circlize in R Studio. I am going through the manual chapters (Circular Visualization in R). The first step is to allocate the sectors on a circle by using the circos.initialize command. However, when I get to this step, I get an error stating missing values where TRUE/FALSE needed.
A reproducible example
library(circlize)
Types <- data.frame(Types = c("OOP", "UVA", "MAT", "OIC", "FIN", "WSE"))
stack.df <- data.frame(Year = c(rep(2019, 1), rep(2020, 4), rep(2021, 7), rep(2022, 11), rep(2023, 11)), Invoice = c(paste0("2019.", "10", ".INV"),
paste0("2020.", seq(from = 20, to = 23, by = 1), ".INV"),
paste0("2021.", seq(from = 30, to = 36, by = 1), ".INV"),
paste0("2022.", seq(from = 40, to = 50, by = 1), ".INV"),
paste0("2023.", seq(from = 50, to = 60, by = 1), ".INV")))
stack.df <- cbind(stack.df, Org_1 = Types[sample(nrow(Types), nrow(stack.df), replace = TRUE), ], Org_2 = Types[sample(nrow(Types), nrow(stack.df), replace = TRUE), ])
Making Chord Diagram
My overall objective: Make a chord diagram where the sectors are the stack.df$Year and track 1 is the stack.df$Invoice, with the circos.links from stack.df$Org_1 to stack.df$Org_2.
Initialize
circos.initialize(sectors = stack.df$Year, x = stack.df$Invoice)
Error in if (sector.range[i] == 0) { :
missing value where TRUE/FALSE needed
In addition: Warning message:
In circos.initialize(sectors = stack.df$Year, x = stack.df$Invoice) :
NAs introduced by coercion
What am I am missing? My sector.range !== 0 as stack.df$Year is from 2019-2023. Any help in overcoming this error is greatly appreciated.

Issues Running the updated version of grts() within the package spsurvey

With the updated version of spsurvey I am having a difficult time trying to generate random points for my design using the grts() function. With the code below I can produce points if n_over = some number and it draws that number across all strata. However I would like a different number of oversamples per stratum based on acres. The first bit of code works and draws the right amount of base points then produces 10 over sample points per stratum
However when I try to do something similar for n_over (Over.test) I receive this error message
" Input Error Message
n_base + n_over : For each stratum, the sum of the base sites and 'Over' replacement sites must be no larger than the number of rows in 'sframe' representing that stratum."
Even though there are 115+ rows per strata in the test.inShape<-st_read("UFO_2022_Ints_Pts_Strata.shp")
There is unfortunately very little information out on the updated spsurvey package.
"ALLOT_NAME" is a column within the shape file that contains the name for each stratum (Adobe, Big Pasture, ect...)
New to Stackoverflow so I apologize if I have not presented this appropriately
### Working code ####
test.prj=c("UFO_2022_Ints_Pts_Strata")
test.inShape<-st_read("UFO_2022_Ints_Pts_Strata.shp")
plots.df<-read.csv("GUSG_Strata.csv")
strata.list<-as.vector(plots.df)
x<-c('Adobe' = 2, 'Big Pasture' = 2, 'Black Ridge' = 45, 'Blue Cimarron' = 2, Buck = 2, 'Crawford Reservoir' = 2, 'Dave Wood Road' = 2, 'Dry Cedar' = 2, 'East Gould Reservoir' = 2, 'Gould Reservoir' = 20, 'Green Mountain - Middle' = 35, 'Grizzly Gulch' = 2, 'Iron Canyon' = 15, 'Little Baldy' = 2, 'Lower Horsefly' = 35, 'Onion Valley' = 2, 'Poison Spring' = 15, 'Rawhide - Coffee Pot' = 2, 'Rim Rock' = 2, 'Shinn Park' = 2, 'Tappan Creek' = 2, 'Green Mountain - Jensen Ware' = 15, 'Green Mountain - West' = 35, 'Green Mountain - East' = 30)
#test.sample<-grts(design=test.design, DesignID="RGFO_2022_RangeLPI", in.shape=test.inShape, id = "RGFO_2022_RangeLPI", prjfilename=test.prj, out.shape="RGFO_2022_RangeLPI_GRTS")
test.sample<-grts(test.inShape, n_base=x, stratum_var="ALLOT_NAME", n_over=10, DesignID="UFO_2022_GUSG" )
test.output<-sp_rbind(test.sample)
st_write(test.output, "UFO_2022_GUSG_SampleDesign_V1_TESTErase.shp")
# will not work when I use n_over = Over.list trying to specify oversample points for each strata
Over.list <- c('Adobe' = 3,
'Big Pasture' = 3,
'Black Ridge' = 10,
'Blue Cimarron' = 3,
'Buck' = 3,
'Crawford Reservoir' = 3,
'Dave Wood Road' = 3,
'Dry Cedar' = 3,
'East Gould Reservoir' = 3,
'Gould Reservoir' = 3,
'Green Mountain - Middle' = 10,
'Grizzly Gulch' = 3,
'Iron Canyon' = 10,
'Little Baldy' = 3,
'Lower Horsefly' = 10,
'Onion Valley' = 3,
'Poison Spring' = 10,
'Rawhide - CoffeePot' = 3,
'Rim Rock' = 3,
'Shinn Park' = 3,
'Tappan Creek' = 3,
'Green Mountain - Jensen Ware' = 10,
'Green Mountain - West' = 10,
'Green Mountain - East' = 10)
### Below is the layout of the grts function
# grts
# sframe,
# n_base,
# stratum_var = NULL,
# seltype = NULL,
# caty_var = NULL,
# caty_n = NULL,
# aux_var = NULL,
# legacy_var = NULL,
# legacy_sites = NULL,
# legacy_stratum_var = NULL,
# legacy_caty_var = NULL,
# legacy_aux_var = NULL,
# mindis = NULL,
# maxtry = 10,
# n_over = NULL,
# n_near = NULL,
# wgt_units = NULL,
# pt_density = NULL,
# DesignID = "Site",
# SiteBegin = 1,
# sep = "-",
# projcrs_check = TRUE
# )
Sorry for the late response here! I think the error you are receiving is the result of a bug in one of our error checks. This bug will be fixed in version 5.4.0 of spsurvey, which should be on CRAN within the next couple weeks. I am hopeful your code will work once the new version is installed. Additionally, spsurvey has a new website viewable here that contains a lot of information about the package.
As for "presenting appropriately" on Stack Overflow, usually you want to provide a reproducible example (more on that here and here). Given the nature of this bug, I think that providing a reproducible example would have been challenging. Thus, I appreciate that you provided all of your code.
EDIT: spsurvey version 5.4.0 was pushed to CRAN on November 22, 2022.

Input error using autoplot and ggplot2: Invalid input: date_trans works with objects of class Date only

library(quantmod)
GDP.growth <- ts(data = getSymbols('A191RL1Q225SBEA', src = 'FRED', auto.assign = F), frequency = 4, start = c(1947,2))
library(ggplot2)
library(forecast)
fit.1 <- arima(ts(GDP.growth[1:259], frequency = 4, start = c(1947,2)), order = c(1,0,0))
summary(fit.1)
fit.2 <- arima(ts(GDP.growth[1:259], frequency = 4, start = c(1947,2)), order = c(3,0,2))
summary(fit.2)
fc.1 <- forecast(ts(GDP.growth[1:259], frequency = 4, start = c(1947,2)), h = 20, model = fit.1)
fc.2 <- forecast (ts(GDP.growth[1:259], frequency = 4, start = c(1947,2)), h = 20, model = fit.2)
autoplot(GDP.growth)+autolayer(fitted(fc.1), color="blue")+autolayer(fc.1, color=alpha("pink", 1/3))
autoplot(GDP.growth)+autolayer(fitted(fc.2), color="blue")+autolayer(fc.2, color=alpha("pink", 1/3))
I get the following message:
Invalid input: date_trans works with objects of class Date only
When I plot only the autoplot it works. It seems the problem lies after the "+". Can anyone help me with this problem?

Change ColorMapper via callback JS in standalone Bokeh plot

I would like to create a standalone document, similar to the server app example 'Crossfilter': to select different columns for coloring the circles and to update the colorbar as well.
I define a CustomJS with the code below, where I create a new LinearColorMapper with the calculated low and high values. (For debugging purposes, I intentionally add different palette than the one set via the Python code).
var low = Math.min.apply(Math,source.data[cb_obj.value]);
var high = Math.max.apply(Math,source.data[cb_obj.value]);
var color_mapper = new Bokeh.LinearColorMapper({palette:'Viridis5', low:low, high:high});
cir.glyph.fill_color = {field: cb_obj.value, transform: color_mapper};
cir.glyph.line_color = {field: cb_obj.value, transform: color_mapper};
color_bar.color_mapper = color_mapper;
source.change.emit();
As a result, when selecting the column, the circles become white, the line black, the ticks of the color bar change correctly, but the palette does not change.
Could you help me setting the proper attributes in the callback? Thank you in advance.
Original state, color mapper set from Python code
After selecting the column 'd'
I created a "minimal working example" to show how far I got. The full project with the template it can be found here: https://github.com/pintergreg/bokehjscolormapperexample
It looks like you cannot reference a colour pallet like this in BokehJS. Just pass Viridis5 variable to JS callback and it works (tested on Bokeh v1.0.4):
import pandas as pd
from bokeh.models import ColumnDataSource, ColorBar, Select, CustomJS
from bokeh.plotting import figure, show
from bokeh.layouts import gridplot
from bokeh.palettes import Spectral5, Viridis5
from bokeh.transform import linear_cmap
from bokeh.embed import components
from jinja2 import Environment, FileSystemLoader
df = pd.DataFrame({"a": [2, 6, 5, 3, 7, 8, 1, 9, 2, 4],
"b": [3, 5, 7, 1, 0, 6, 5, 4, 2, 9],
"c": [11, 12, 13, 14, 11, 13, 15, 14, 15, 12],
"d": [21, 23, 24, 25, 21, 22, 23, 24, 25, 22]})
source = ColumnDataSource(df)
mapper = linear_cmap(field_name = "c", palette = Spectral5,
low = min(df["c"]), high = max(df["c"]))
fig = figure(plot_width = 400, plot_height = 400)
cir = fig.circle(x = "a", y = "b", size = 12,
source = source, line_color = mapper, color = mapper)
color_bar = ColorBar(color_mapper = mapper["transform"], width = 8,
location = (0, 0))
fig.add_layout(color_bar, "right")
codec = """
var low = Math.min.apply(Math,source.data[cb_obj.value]);
var high = Math.max.apply(Math,source.data[cb_obj.value]);
var color_mapper = new Bokeh.LinearColorMapper({palette:viridis5, low:low, high:high});
cir.glyph.fill_color = {field: cb_obj.value, transform: color_mapper};
cir.glyph.line_color = {field: cb_obj.value, transform: color_mapper};
color_bar.color_mapper.low = low;
color_bar.color_mapper.high = high;
color_bar.color_mapper.palette = viridis5;
source.change.emit();
"""
cb_cselect_c = CustomJS(args = dict(cir = cir, source = source, color_bar = color_bar, viridis5 = Viridis5),
code = codec)
c_select = Select(title = "Select variable for color: ", value = "None",
options = ["c", "d"], callback = cb_cselect_c)
layout = gridplot([[fig], [c_select]])
show(layout)
# env = Environment(loader=FileSystemLoader("."))
# template = env.get_template("template.html")
#
# script, div = components(layout)
#
# with open("output.html", "w") as f:
# print(template.render(script=script, div=div), file=f)
You also need to manually add this line to header section of the generated HTML file:
<script type="text/javascript" src="http://cdn.bokeh.org/bokeh/release/bokeh-api-1.0.4.min.js"></script>

Plotting with Vennerable package in R

Here is a very basic example:
library(vennerable)
srl.venn <- Venn(SetNames=c("Cognitive condition","Operations","Individual differences"),
Weight=c(0,30, 21, 15, 1, 8, 3, 6))
plot(srl.venn)
All I'm trying to do is to remove borders around circles, and format colors and fonts. However, still haven't done much.
Could you please share any useful examples?
Check out VennThemes for changing parameters within the plot. For example:
library(Vennerable)
srl.venn <- Venn(SetNames=c("Cognitive condition","Operations","Individual differences"),
Weight=c(0,30, 21, 15, 1, 8, 3, 6))
srl.venn.c <- compute.Venn(srl.venn, doWeights=T)
gp <- VennThemes(srl.venn.c, colourAlgorithm = "binary")
plot(srl.venn.c, gpList = gp, show = list(FaceText = "signature", SetLabels = FALSE,
Faces = FALSE, DarkMatter = FALSE))
More detail can be found in the man pages or by calling vignette("Venn")

Resources