Longitudinal data plotting

Hello people,

I’m trying to plot longitudinal data that includes columns “ID”, “Time” and “Weight”. When I try to plot it with command plot(df.Time, df.Weight) it creates one line that goes back and forth. So how I can split the legends by ID?

Example image with zigzag lines:

Here is code to generate the problem:

using DataFrames
using Random
using Statistics
using Distributions
using Plots

n = 50
left_ratio = 5; right_ratio = convert(Int, n * 1/left_ratio)
ID = repeat(1:right_ratio,inner=left_ratio)
Time = repeat(1:left_ratio, outer=right_ratio)
beta0 = 5; beta1 = 2
error = 10
b0_random_effect_variance= 10
b0_random_effect = repeat(rand(Normal(0, sqrt(b0_random_effect_variance)),right_ratio),inner=left_ratio)

y = (beta0 .+ b0_random_effect) .+ (beta1 .* Time) .+ rand(Normal(0, sqrt(error)), n)
full_df = DataFrame(ID=ID, Time=Time, Weight=y)
plot(full_df.Time, full_df.Weight)

Oh my god.
I solved my own problem by following Julia forums sophisticated recommendations.

plot(full_df.Weight, group=full_df.Time)

Does this mean I have to move to live rest of my life in exilement or should I remove the post?

Ok, that didn’t solve my problem. Because it groups by Time (like the code says). When I use “plot(full_df.Weight, group=full_df.ID)”
It prints like this:


Why not:

plot(full_df.Time, full_df.Weight, group=full_df.ID)
1 Like

Interesting. That solved my problem. Thanks!
I tried that at one point but maybe my DataFrame had problems at that point or something.

Longitudinal because the data is “long format”. I could have used other more common terms but I hope people interested in longitudinal data analysis could find my post in the future.