I wrote a function that plots several boxplots, ordered by the orders of a factor (categorical variable). It works, but VSCode keeps telling me there is a problem in one of the lines. Would someone know the reason?
Here is the code:
using DataFrames, StatsPlots
varnames = ["Winter", "Spring", "Summer", "Fall"]
df = DataFrame(
:variable => categorical(repeat(varnames, 20), levels = varnames),
:value => rand(80)
)
function boxplot_ordered(
df::DataFrame;
variable_col::Symbol = :variable,
value_col::Symbol = :value,
plotsize::Tuple{Int64,Int64} = (800, 600) # ctrl_parms.PlotSize
)
msg = "`variable_col` is not a column name of `df`"
@assert string(variable_col) in names(df) msg
msg = "`value_col` is not a column name of `df`"
@assert string(value_col) in names(df) msg
msg = "Column `variable_col` of `df` is not categorical"
@assert typeof(df[!, variable_col]) <: CategoricalVector msg
variable_order = string.(levels(df[!, variable_col]))
v = collect(1:length(variable_order))
d = Dict(zip(variable_order, v))
dfc = select(
df,
variable_col => ByRow(x -> d[x]) => :Order,
variable_col => (x -> (string.(x))) => variable_col,
value_col,
)
p = boxplot(
dfc.Order,
dfc[!, value_col],
legend = false,
size = plotsize
)
plot!(xticks = (v, variable_order)) # THIS IS THE LINE FLAGGED AS A PROBLEM
display(p)
return p
end # boxplot_ordered
boxplot_ordered(df)