Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: geom_smooth select observations connections (equivalence to geom_path())

I am using ggplot2 to create vertical profiles of the ocean. My raw data set creates "spikes" so to make smooth curves. I am hoping to use geom_smooth(). I also want the line to progress according to the order of the observations (and not according to the x axis). When I use geom_path(), it works for the original plot, but not for the resulting geom_smooth() (see picture below).

melteddf = Storfjorden %>% melt(id.vars = "Depth")
ggplot(melteddf, aes(y = Depth, x = value)) + 
  facet_wrap(~ variable, nrow = 1, scales = "free_x") + 
  scale_y_reverse() +
  geom_smooth(span = 0.5,se = FALSE) + 
  geom_path()

enter image description here Therefore is there a way to make sure the smooth curve progress according to the order of observations, instead of the a axis?

Subset of my data:

head(Storfjorden)
      Depth Salinity Temperature Fluorescence
    1  0.72    34.14       3.738         0.01
    2  0.92    34.14       3.738         0.02
    3  1.10    34.13       3.739         0.03
    4  1.80    34.14       3.740         0.06
    5  2.80    34.13       3.739         0.02
    6  3.43    34.14       3.739         0.05
like image 674
Vincent La Foote Carrier Avatar asked Jan 01 '26 14:01

Vincent La Foote Carrier


1 Answers

The data that you provided is quite minimal, but we can make it work.

Using some of the tidyverse packages we can fit separate loess functions to each of the variables.

What we do, essentially, is

  1. Group our data by variable (group_by).
  2. Use do to fit a loess function to each group.
  3. Use augment to create predictions from that loess model, in this case for a 1000 values within the range of the data (for that variable).

.

# Load the packages
library(dplyr)
library(broom)

lo <- melteddf %>% 
  group_by(variable) %>% 
  do(augment(
    loess(value ~ Depth, data = .), 
    newdata = data.frame(Depth = seq(min(.$Depth), max(.$Depth), l = 1000))
  ))

Now we can use that predicted data in a new geom_path call:

ggplot(melteddf, aes(y = Depth, x = value)) + 
  facet_wrap(~ variable, nrow = 1, scales = "free_x") + 
  scale_y_reverse() +
  geom_path(aes(col = 'raw')) +
  geom_path(data = lo, aes(x = .fitted, col = 'loess'))

(I map simple character vectors to the color of both lines to create a legend.)

Result:

enter image description here

like image 200
Axeman Avatar answered Jan 03 '26 03:01

Axeman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!