# How to visualize trends

I am working on a paper where we plotted BMI trends as a function of age in the population. We plotted trends for six databases, then we plotted for each sex, then for race, in three categories. I mean that I have a lot of trend lines, and I have no idea how to summarize in a few graphs all the trends plotted and where to look for suggestions.

Punctually, the median IMC is plotted for each age of the people, it makes a maximum peak and then goes down. I would like to be able to show the different trends for each population under study (there are 6). The trends were fitted to a second degree polynomial curve. My idea would be to show a graph with 6 lines for the general population, which i don't know how to do. I would also like to show the trends by sex and race.

Could someone give me a hand, suggesting some examples of how to make these graphs?

• What are the top 2-3 things you want your audience to learn from these summary graphs? Commented Nov 1, 2023 at 15:03
• punctually, the median imc is plotted for each age of the people, it makes a maximum peak and then goes down. i would like to be able to show the different trends for each population under study (there are 6) the trends were fitted to a second degree polynomial curve. my idea would be to show a graph with 6 lines for the general population, which i don't know how to do. i would also like to show the trends by sex and race. Commented Nov 1, 2023 at 15:09
• You can probably combine the six databases. Then make a graph for sex and a graph for race. Commented Nov 1, 2023 at 15:57
• "Small multiple" is a good technique and good keyword for further research.
– whuber
Commented Nov 1, 2023 at 21:35

I would plot a lowess of BMI and age for each of the six race X sex groups.

Here is an example of women's hourly wages and age cut by marital status and race. My analog of DB is time group, defined by how many hours the woman worked during each week, binned.

If the goal is to compare demographics within each time group, I would do this:

If the goal is to compare the same demographic group across time groups, I would swap like this:

You could also do the dashed reference line for the whole dataset rather than the lines in that panel.

There are some odd things happening in the sparse panels where we don't have a lot of data (like single and married others who work a lot).

• +1. I think personally I am not as much a fan of putting too much color if it can distract people (particularly with a multi-factor approach to analysis). I provide a facet grid example in my answer because IMO it simplifies what you see. But that is more a personal preference than a statistical one, and I generally think your answer still hits the mark. Commented Nov 2, 2023 at 1:37
• @ShawnHemelstrand I agree that labeling the lines would be better. In the example I picked, the lines were so jumbled it would have been hard to operationalize. I also find it easier to make comparisons when the lines share the same axes. Commented Nov 2, 2023 at 2:57
• That certainly makes sense. Commented Nov 2, 2023 at 3:02

#### Thinking About Plotting

I think this will depend on your goal. If your goal is to simply explain the data you have in front of you and not make any inferential claims, then a simple line graph will work. This won't actually approximate the functional relationships between your IVs and DVs in the most direct way, but will explain plainly what you see in the data.

If you are instead trying to make some claim about the relationships between these variables in your target population (which I assume to be the case for you), then like you and some others have noted, a nonlinear fitting with polynomials, LOESS, or splines may be useful. This will depend on what the data looks like and how you want to tackle under/overfitting your data. Polynomials tend to interpolate poorly at the ends of a distribution (particularly with very high-order fits) and LOESS can overweight "heavy" parts of the distribution (leading to poor placement of the LOESS line), so I would be more inclined to model this with a spline, as they tend to be more flexible and penalize overfitting. However, if the data can fit simpler methods (which by your description seems to be the case), then I don't see a problem with any of these methods and I would pick the method that is simplest/you are most comfortable with. It seems like the polynomial fit would be fine in your case but I also don't have your data on hand to comment with great confidence on that point.

Peter noted that you can probably just plot all of the six databases together. I think this is okay if there are not systematic differences between your databases, but if there are some substantial ecological confounds related to each of the six samples, then I would examine that and see if problems arise.

#### Simulated Example

Here is some simulated data that I used in R, which I use to simulate how age is associated with BMI nonlinearly. Notice that this is not simulated to capture by-database differences, so this may look different if that is the case.

#### Load Libraries and Seed ####
library(tidyverse)
set.seed(123)

#### Define Parameters ####
n <- 1000
ages <- seq(1, 20, length.out = n)
sexes <- c("Male", "Female")
races <- c("Race1", "Race2", "Race3")

#### Generate Data ####
data <- expand.grid(Age = ages,
Sex = sexes,
Race = races)

#### Create BMI Trend Function ####
bmi_trend <- function(age) {
if (age <= 10) {
return(15 + age * 0.5 + rnorm(1, sd = 1))
} else {
return(20 + (age-10) * 2 + rnorm(1, sd = 2))
}
}

#### Apply to Data ####
data$$BMI <- sapply(data$$Age, bmi_trend)

#### Add "Noise" ####
data$$BMI[data$$Sex == "Female"] <- data$$BMI[data$$Sex == "Female"] + rnorm(sum(data$$Sex == "Female"), sd = 1) data$$BMI[data$$Race == "Race2"] <- data$$BMI[data$$Race == "Race2"] + rnorm(sum(data$$Race == "Race2"), sd = 1)
data$$BMI[data$$Race == "Race3"] <- data$$BMI[data$$Race == "Race3"] + rnorm(sum(data\$Race == "Race3"), sd = 2)

#### Plot Data ####
ggplot(data,
aes(x = Age,
y = BMI)) +
geom_point(alpha = 0.2,
color="steelblue",
size=3) +
geom_smooth(se=F,
color="black",
method="loess")+
theme_minimal() +
labs(title = "Simulated BMI trends as a function of age by sex and race",
x = "Age (years)",
y = "BMI",
color = "Sex",
shape = "Race")+
facet_grid(Sex ~ Race)


Here you get a facet grid with some simple LOESS lines.

Note that a polynomial, LOESS, or spline fit here would likely all look the same given the simplicity of the fit. For something that oscillates wildly, a LOESS or spline is likely better, but your data sounds like it is closer to what I have simulated above.

You can visualize the trends using some splines, for example thin plate regression splines (TPRS) from the mgcv package in R, using something like this syntax:

BMI ~ SEX + s(age, by=SEX)


But this is just a suggestion and also the distribution of the index value might be considered to have an appropriate link function.

• The distribution of the index value might be considered to have an appropriate link function---Could you explain what you mean by this? Do you mean how to model the error term in GAMs? This also doesn't account for the race trend, nor does it show how to visualize the relationship, so I would add more here. Commented Nov 2, 2023 at 1:33