# Longitudinal data plotting

**URL:** https://discourse.julialang.org/t/longitudinal-data-plotting/105629
**Category:** New to Julia
**Tags:** plots
**Created:** [October 31, 2023, 7:01pm UTC](https://discourse.julialang.org/t/longitudinal-data-plotting/105629 "2023-10-31T19:01:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Huputus](https://avatars.discourse-cdn.com/v4/letter/h/b2d939/32.png) [@Huputus](https://discourse.julialang.org/u/Huputus)
#### Post date: [October 31, 2023, 7:01pm UTC](https://discourse.julialang.org/t/longitudinal-data-plotting/105629/1 "2023-10-31T19:01:30Z")

</div>

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:

 ![plotted](https://global.discourse-cdn.com/julialang/original/3X/0/d/0d2d14398c5beb62f1a7e1c5b9150cc407c96461.jpeg)

Here is code to generate the problem:

```julia
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)

```

---

<div class="post-metadata">

### Author: ![Huputus](https://avatars.discourse-cdn.com/v4/letter/h/b2d939/32.png) [@Huputus](https://discourse.julialang.org/u/Huputus)
#### Post date: [October 31, 2023, 7:56pm UTC](https://discourse.julialang.org/t/longitudinal-data-plotting/105629/2 "2023-10-31T19:56:26Z")

</div>

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

```julia
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?

---

<div class="post-metadata">

### Author: ![Huputus](https://avatars.discourse-cdn.com/v4/letter/h/b2d939/32.png) [@Huputus](https://discourse.julialang.org/u/Huputus)
#### Post date: [October 31, 2023, 8:08pm UTC](https://discourse.julialang.org/t/longitudinal-data-plotting/105629/3 "2023-10-31T20:08:27Z")

</div>

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:

 ![groupID](https://global.discourse-cdn.com/julialang/original/3X/7/4/745c82d34dc0604d82e0f3466aaaf4cd0183ea6f.jpeg)  
:

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 31, 2023, 8:27pm UTC](https://discourse.julialang.org/t/longitudinal-data-plotting/105629/4 "2023-10-31T20:27:16Z")

</div>

Why not:

```julia
plot(full_df.Time, full_df.Weight, group=full_df.ID)

```

---

<div class="post-metadata">

### Author: ![Huputus](https://avatars.discourse-cdn.com/v4/letter/h/b2d939/32.png) [@Huputus](https://discourse.julialang.org/u/Huputus)
#### Post date: [October 31, 2023, 8:36pm UTC](https://discourse.julialang.org/t/longitudinal-data-plotting/105629/5 "2023-10-31T20:36:27Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Huputus](https://avatars.discourse-cdn.com/v4/letter/h/b2d939/32.png) [@Huputus](https://discourse.julialang.org/u/Huputus)
#### Post date: [October 31, 2023, 8:45pm UTC](https://discourse.julialang.org/t/longitudinal-data-plotting/105629/7 "2023-10-31T20:45:10Z")

</div>

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.
