I am new in Julia and coming from R. In R I somehow master plotting with {ggplot2} and I am very used to work with tidy data frames.
Now that I am starting the PhD thesis I am considering moving to Julia.
I must say I have struggled a bit these last two days to make a graphical analysis that in R took me half-an-hour to do… Specially because I found the documentation of PlotlyJS not very complete, I must say.
I ended up with a good final result, but I would love to hear comments and suggestions from the community to improve my code.
The result is this panel of plots:
My code is:
function plot_setup!(df::DataFrame, c::Vector{String}, vars::Vector{String})
df = filter([:country, :variable, :year] => (x, y, z) -> x in(c) && y in(vars) && z >= Date(1950), df)
df
end;
sel_countries = ["Portugal", "Italy", "Greece", "Germany", "United States", "Canada", "South Africa", "India", "United Kingdom"]
sel_variables = ["r", "implicit_r"]
plot_df = plot_setup!(df, sel_countries, sel_variables)
function country_ts_plot(df::DataFrame, c::String)
trace = scatter(
filter(:country => ==(c), df),
x = :year,
y = :value,
group = :variable,
mode = "lines"
)
layout = Layout(
title_text = c
)
p = PlotlyJS.SyncPlot
p = plot(
trace,
layout
)
restyle!(p, 2, name = "Implicit r")
p
end;
plot_us = country_ts_plot(plot_df, "United States")
plot_pt = country_ts_plot(plot_df, "Portugal")
plot_it = country_ts_plot(plot_df, "Italy")
plot_de = country_ts_plot(plot_df, "Germany")
plot_gr = country_ts_plot(plot_df, "Greece")
plot_ca = country_ts_plot(plot_df, "Canada")
plot_uk = country_ts_plot(plot_df, "United Kingdom")
plot_in = country_ts_plot(plot_df, "India")
plot_za = country_ts_plot(plot_df, "South Africa")
panel = [plot_us plot_pt plot_it
plot_de plot_gr plot_ca
plot_uk plot_in plot_za]
restyle!(panel, 1:18, name = ["r", "Implicit r"], line_color = ["dodgerblue", "crimson"])
restyle!(panel, 3:18, showlegend = false)
relayout!(panel, Dict(:legend => Dict(:orientation => "h")))
panel
The first attempt with a single call of plot()
and using facet_col
attribute was not successful. The result was this:
The second, already with the function country_ts_plot()
was giving this result: