``` r
# Put predictor expression in format with samples in columns
full_pred_counts_t <- t(full_pred_counts)

# Loop over each feature in the list
for (feature in names(top_predictors)) {
  
  # Get top 5 predictors for this feature
  top_df <- top_predictors[[feature]]
  predictors <- top_df$Predictor
  
  # Combine feature and predictors
  all_genes <- c(feature, predictors)
  
  # Check which genes are actually present
  present_genes <- all_genes[all_genes %in% rownames(full_pred_counts_t) | all_genes %in% rownames(vsd_energetic_state_GO)]
  
  if (!(feature %in% rownames(vsd_energetic_state_GO))) {
    warning(paste("Feature", feature, "not found in vsd_energetic_state_GO Skipping..."))
    next
  }

  # Extract predictor expression (only those present)
  predictor_expr <- full_pred_counts_t[intersect(predictors, rownames(full_pred_counts_t)), , drop = FALSE] %>% as.data.frame()

  # Add the feature expression
  feature_expr <- vsd_energetic_state_GO[feature, , drop = FALSE] %>% as.data.frame()
  
  # Combine into one data frame
  combined_expr <- rbind(predictor_expr, feature_expr)
  combined_expr$Gene <- rownames(combined_expr)
  
  # Convert to long format
  expr_long <- combined_expr %>%
    pivot_longer(-Gene, names_to = "Sample", values_to = "Expression")
  
  # Join with sample metadata
  expr_long <- expr_long %>%
    left_join(metadata, by = "Sample")
  
  # Plot, with colonies aggregated by timepoint
  # p <- ggplot(expr_long, aes(x = interaction(Timepoint), y = Expression, color = Gene, group = Gene)) +
  #   geom_point(alpha = 0.7) +
  #   geom_smooth(se = FALSE, method = "loess") +
  #   labs(title = paste("Expression of", feature, "and Top 5 Predictors"),
  #        x = "Colony-Timepoint",
  #        y = "Expression") +
  #   theme_minimal() +
  #   theme(axis.text.x = element_text(angle = 45, hjust = 1))
  
  # Plot, faceted by colony
  p <- ggplot(expr_long, aes(x = Timepoint, y = Expression, color = Gene, group = Gene)) +
  geom_point(alpha = 0.7) +
  geom_smooth(se = FALSE, method = "loess", linewidth = 0.8) +
  facet_wrap(~ColonyID) +
  labs(title = paste("Expression of", feature, "and Top 5 Predictors by Colony"),
       x = "Timepoint",
       y = "Expression") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

  
  print(p)
}
```

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-1.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-2.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-3.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-4.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-5.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-6.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-7.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-8.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-9.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-10.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-11.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-12.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-13.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-14.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-15.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-16.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-17.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-18.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-19.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-20.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-21.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-22.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-23.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-24.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-25.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-26.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-27.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-28.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-29.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-30.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-31.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-32.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-33.png)<!-- -->

```
## `geom_smooth()` using formula = 'y ~ x'
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-72-34.png)<!-- -->