I am trying to plot a dataframe I created from an existing dataframe. Basically I just took about 5 rows and all N-4 columns and created a new dataframe. I wanted to plot this but Julia (on Pluto.jl) is throwing this error:
Cannot convert Array{Any,2} to series data for plotting
error(::String)@error.jl:33
_prepare_series_data(::Array{Any,2})@series.jl:8
_series_data_vector(::Array{Any,2}, ::Dict{Symbol,Any})@series.jl:27
macro expansion@series.jl:144[inlined]
apply_recipe(::AbstractDict{Symbol,Any}, ::Type{RecipesPipeline.SliceIt}, ::Any, ::Any, ::Any)@RecipesBase.jl:282
_process_userrecipes!(::Any, ::Any, ::Any)@user_recipe.jl:36
recipe_pipeline!(::Any, ::Any, ::Any)@RecipesPipeline.jl:70
_plot!(::Plots.Plot, ::Any, ::Any)@plot.jl:172
#plot#129@plot.jl:58[inlined]
#add_label#17(::Base.Iterators.Pairs{Symbol,Any,NTuple{13,Symbol},NamedTuple{(:label, :xlabel, :ylabel, :xticks, :xrotation, :marker, :line, :legend, :grid, :framestyle, :legendfontsize, :tickfontsize, :formatter),Tuple{Array{String,2},String,String,Array{Dates.Date,1},Int64,Tuple{Symbol,Int64},Tuple{Symbol,String},Symbol,Bool,Symbol,Int64,Int64,Symbol}}}, ::typeof(StatsPlots.add_label), ::Array{Any,1}, ::Function, ::Array{Dates.Date,1}, ::Vararg{Any,N} where N)@df.jl:155
(::Main.workspace140.var"#1#3")(::DataFrames.DataFrame)@range.jl:0
top-level scope@Local: 17
This is the cell that is throwing this error:
begin
countries = ["Italy", "Germany", "India", "United Kingdom"];
y = DataFrame() # empty dataframe
for country in countries
data_dfr = get_country(df,country); # returns a dataframe row
data_dfr = DataFrame(data_dfr); # convert dataframe row back to a dataframe
df_rows, df_cols = size(data_dfr);
data_dfl = stack(data_dfr, 5:df_cols); # convert dataframe into long format
y[!,Symbol("$country")] = data_dfl[!,:value]
end
rows,cols = size(y)
gr(size=(900,600))
@df y plot(x_axis, cols(1:cols),
label = reshape(names(y),(1,length(names(y)))),
xlabel = "Time",
ylabel = "Total number of reported cases",
xticks = x_axis[1:7:end],
xrotation = 45,
marker = (:diamond,4),
line = (:line, "gray"),
legend = :topleft,
grid = false,
framestyle = :semi,
legendfontsize = 9,
tickfontsize = 9,
formatter = :plain
)
y.One_million = Array{Union{Missing,Float64},1}(missing,size(y,1));
y.One_million .= 10^6.0;
display(@df y plot!(x_axis, y[!,cols+1],
linestyle = :dot,
linewidth = 5,
color = :red,
label = names(y)[cols+1]))
y = select!(y, Not([:One_million]));
end
Here are the functions and variables used in the above code:
begin
dates = names(df)[begin:end-4]
date_format = Dates.DateFormat("m/d/y")
x_axis = parse.(Date, dates, date_format) .+ Year(2000)
end
function get_country(dataframe, country::String)
df_country = dataframe[ismissing.(dataframe[!, Symbol("Province/State")]), :]
indx = findfirst(df_country[!, Symbol("Country/Region")] .== country)
return df_country[indx, :]
end
begin
dataset = download("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv","covid_19_global_data.csv")
df = CSV.read("covid_19_global_data.csv", DataFrame)
write_parquet("covid_19_global_data.parquet", df)
df = DataFrame(read_parquet("covid_19_global_data.parquet"))
Arrow.write("covid_19_global_data.arrow", df)
df = DataFrame(Arrow.Table("covid_19_global_data.arrow"))
end