1

I have dataframe with an id column. I want to filter the dataframe for a specific id and then add data from columns to a plot (This part is done using a for loop). And finally, I want to add the plots (3 in this case) into a subplot. I have achieved it like this, but the plot I end up with is incorrect. Wondering if anyone has an idea what I am doing wrong (top two subplots are empty, all info seem to be in the third subplot)?

function plotAnom(data::DataFrame)
data = copy(data)
uniqIds = unique(data.id)

# Create emplty plots
p1 = plot()
p2 = plot()
p3 = plot()

for id in uniqIds
    # Filter datframe based on id
    df = data[data.id .== id,:]
    p1 = plot!(
        df.time,
        df[:,names(df)[3]],
        label = id,
        line = 3,
        legend = :bottomright,
    )
    p1 = plot!(
        df.time,
        df.pre_month_avg,
        label = id,
        line = 2
    )
    
    p2 = plot!(
        df.time,
        df.diff,
        line = 3
    )
    
    p3 = plot!(
        df.time,
        df.cumulative_diff,
        line = 3
    )
    
    end

p = plot(p1,p2,p3,layout = (3,1), legend = false)
return p
end

enter image description here

imantha
  • 2,676
  • 4
  • 23
  • 46

1 Answers1

2

In your example, you always update the latest created plot, which is your p3. When you use plot!, you specify which plot gets updated by putting it in plot!'s arguments (otherwise it updates the latest one). So I think you should do plot!(p1, ...) instead of p1 = plot!(...), and so on.

Benoit Pasquier
  • 2,868
  • 9
  • 21