I think I’m using too many libraries and I can’t get two lines on the same plot.
using Pkg
Pkg.add("DataFrames")
Pkg.add("Gadfly")
Pkg.add("Dates")
Pkg.add("StatsBase")
Pkg.add("Query")
Pkg.add("Polynomials")
Pkg.add("Missings")
using DataFrames, Gadfly, Dates, LibPQ, StatsBase, Query, Polynomials
using Missings
# Connect to the database
conn = LibPQ.Connection("postgresql://postgres:mypw@localhost:5433/financials")
# Execute the SQL query to retrieve the data
df = DataFrames.DataFrame(execute(conn, """
SELECT "Date", "Amount", "Cat2"
FROM "2022_finances"
WHERE "Cat1" = 'Income'
"""))
# Close the connection
close(conn)
# Convert the 'Date' column to a DateTime object
df.Date = Dates.DateTime.(df.Date)
grouped = df |>
@groupby(month(_.Date)) |>
@map({Month=key(_), Amount=sum(_.Amount)}) |>
DataFrame
# Generate the extrapolation
grouped.Month = convert(Array{Float64,1}, grouped.Month)
x = coalesce.(grouped.Month)
y = coalesce.(grouped.Amount)
p = Polynomials.fit(x, y, 3) # can't get this work without the parent class
x_float = convert(Array{Float64, 1}, collect(13:24))
y_float = p.(x_float)
# Plot the data
df_extrapolation = DataFrame(Month=x_float, Amount=y_float)
x = df_extrapolation[!, :Month]
y = df_extrapolation[!, :Amount]
p = plot()
# Plot the original data
plot(grouped, x=:Month, y=coalesce.(:Amount), Geom.line(),
Guide.xlabel("Month"), Guide.ylabel("Amount (\$)"))
# Plot the extrapolation
plot!(df_extrapolation, x=:Month, y=:Amount, Geom.line(), label="Extrapolation")
ERROR: Cannot convert DataFrame to series data for plotting
I’ve tried so many different inputs and can’t the plots to combine.