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

放肆的年华 提交于 2019-12-01 06:16:39

问题


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()

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

回答1:


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:



来源:https://stackoverflow.com/questions/39476914/ggplot2-geom-smooth-select-observations-connections-equivalence-to-geom-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!