Reputation: 199
I am looking for a way to reshape a DataFrame from wide to long and then plot the results (this should be a simple operation, but I am new to Julia and not an expert programmer by any means).
Specifically, I have a data frame with the following structure:
julia> df = DataFrame(Country = ["Italy","France","Germany"], Date1 = [1,4,6], Date2 = [2,5,9], Date3 = [4,3,12])
3×4 DataFrame
│ Row │ Country │ Date1 │ Date2 │ Date3 │
│ │ String │ Int64 │ Int64 │ Int64 │
├─────┼─────────┼───────┼───────┼───────┤
│ 1 │ Italy │ 1 │ 2 │ 4 │
│ 2 │ France │ 4 │ 5 │ 3 │
│ 3 │ Germany │ 6 │ 9 │ 12 │
I have successfully used the stack()
function to reshape the data as follows:
julia> df_long = stack(df,2:4)
9×3 DataFrame
│ Row │ variable │ value │ Country │
│ │ Symbol │ Int64 │ String │
├─────┼──────────┼───────┼─────────┤
│ 1 │ Date1 │ 1 │ Italy │
│ 2 │ Date1 │ 4 │ France │
│ 3 │ Date1 │ 6 │ Germany │
│ 4 │ Date2 │ 2 │ Italy │
│ 5 │ Date2 │ 5 │ France │
│ 6 │ Date2 │ 9 │ Germany │
│ 7 │ Date3 │ 4 │ Italy │
│ 8 │ Date3 │ 3 │ France │
│ 9 │ Date3 │ 12 │ Germany │
Now I wish to create a plot with the variable
column on the x-axis and the value
column on the y-axis. However, the variable
column has type Symbol (rather than String as I was hoping), so I am unable to plot it. The code I am using to create the plot is this:
julia> Plots.plot(df_long.variable,df_long.value)
ERROR: Cannot convert Symbol to series data for plotting
Stacktrace:
[1] prepareSeriesData(::Symbol) at /Users/kayvon/.julia/packages/Plots/12uaJ/src/series.jl:14
[2] convertToAnyVector(::Symbol, ::Dict{Symbol,Any}) at /Users/kayvon/.julia/packages/Plots/12uaJ/src/series.jl:27
[3] (::Plots.var"#152#155"{Dict{Symbol,Any}})(::Symbol) at ./none:0
[4] iterate(::Base.Generator{Array{Symbol,1},Plots.var"#152#155"{Dict{Symbol,Any}}}) at ./generator.jl:47
[5] convertToAnyVector(::Array{Symbol,1}, ::Dict{Symbol,Any}) at /Users/kayvon/.julia/packages/Plots/12uaJ/src/series.jl:42
[6] macro expansion at /Users/kayvon/.julia/packages/Plots/12uaJ/src/series.jl:130 [inlined]
[7] apply_recipe(::Dict{Symbol,Any}, ::Type{Plots.SliceIt}, ::Array{Symbol,1}, ::Array{Int64,1}, ::Nothing) at /Users/kayvon/.julia/packages/RecipesBase/G4s6f/src/RecipesBase.jl:279
[8] _process_userrecipes(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{Array{Symbol,1},Array{Int64,1}}) at /Users/kayvon/.julia/packages/Plots/12uaJ/src/pipeline.jl:85
[9] _plot!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{Array{Symbol,1},Array{Int64,1}}) at /Users/kayvon/.julia/packages/Plots/12uaJ/src/plot.jl:178
[10] #plot#138(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(plot), ::Array{Symbol,1}, ::Vararg{Any,N} where N) at /Users/kayvon/.julia/packages/Plots/12uaJ/src/plot.jl:57
[11] plot(::Array{Symbol,1}, ::Array{Int64,1}) at /Users/kayvon/.julia/packages/Plots/12uaJ/src/plot.jl:51
[12] top-level scope at none:0
Is there a way to use stack()
that results in the variable
column with type String
? Or am I going about this the wrong way / there is a simpler way?
Thank you, I appreciate any help you can give!
Upvotes: 2
Views: 1819
Reputation: 42244
plot(String.(df_long.variable),df_long.value)
Note the dot .
, which is Julia's dot operator that converts the entire vector of Symbol
s to a vector of String
s.
However for this data you will probably like more a scatter plot.
scatter(String.(df_long.variable),df_long.value)
Upvotes: 2