Pre-smoothing the data before ggplot2. Default geom_smooth values

47 views Asked by At

I'm trying to plot cluster progression over time (age). This is my simplified code

cluster_merged <- mat_long %>%
  ggplot(aes(age, counts, group = FALSE)) +
  geom_smooth(aes(age, mean_exp, group = cluster_label, 
                  color = cluster_label),
              data = cluster_means,
              size = 2.5,
              se = FALSE,
              method = "loess") 


cluster_merged 

Head of my cluster_meansHead of my cluster_means

I am trying to animate the plot using gganimate. I want to do it using

cluster_merged +
  transition_reveal(age)

animate(cluster_merged, duration = 10, fps = 20, width = 2000, height = 680, renderer = gifski_renderer())

anim_save("Clusters Animated.gif")

However, this is incompatible with geom_smooth. It does work with geom_line, and my idea was to apply pre-smoothing loess to my data, and then plot the predicted values with geom_line.

smoothed_data <- cluster_means %>%
  group_by(cluster_label) %>% 
  do({
    # Apply LOESS smoothing within each group with parameters aligned to geom_smooth defaults
    model <- loess(mean_exp ~ age, data = ., span = 0.75)
    data.frame(age = .$age, mean_exp_smooth = predict(model, newdata = data.frame(age = .$age), se = FALSE))
  }) %>%
  ungroup()

And then plot:

cluster_merged2 <- smoothed_data %>%
  ggplot(aes(age, mean_exp_smooth, color = cluster_label)) + 
  geom_line(size = 2.5)

This works, but my clusters are annoyingly slighly different:

geom_smooth version loess version

I suspect that geom_smooth default parameters are somehow different from what I'm writing out in a direct loess call. Does anybody have any insights / knows a simpler way to solve this? Thanks a lot in advance.

I also tried

smoothed_data <- cluster_means %>%
  group_by(cluster_label) %>%
  do({
    # Apply LOESS smoothing within each group using parameters similar to geom_smooth defaults
    # Assuming fewer than 1000 observations per group, thus using LOESS
    model <- loess(mean_exp ~ age, data = ., span = 0.75)
    # Generate a sequence of ages for smooth predictions
    age_seq <- seq(min(.$age), max(.$age), length.out = 80)
    # Predict mean_exp using the model over the sequence
    data.frame(age = age_seq, mean_exp_smooth = predict(model, newdata = data.frame(age = age_seq)))
  }) %>%
  ungroup()

With no success

0

There are 0 answers