3

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
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
  • 2
    Could you include a subset of your data (using `dput` and `head`) so that this is [more reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – bouncyball Sep 13 '16 at 18:45
  • I hope the edit I did correspond to what you were asking. Thanks! – Vincent La Foote Carrier Sep 13 '16 at 19:00
  • 1
    You can try using `coord_flip` and swapping x and y, but facets tend to give some troubles with that. Otherwise you will probably have to do the smoothing outside of `ggplot` first. Have a look at `?loess`. – Axeman Sep 13 '16 at 20:14
  • 1
    I did consider `loess`but then I struggle using a loess object in geom_smooth. But if you have insights using loess I will be really glad to hear about! – Vincent La Foote Carrier Sep 13 '16 at 20:31

1 Answers1

2

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

Community
  • 1
  • 1
Axeman
  • 32,068
  • 8
  • 81
  • 94