ggplot2:geom_smooth select观察连接(与geom_path()等价) [英] ggplot2: geom_smooth select observations connections (equivalence to geom_path())

查看:191
本文介绍了ggplot2:geom_smooth select观察连接(与geom_path()等价)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ggplot2 创建海洋垂直剖面。我的原始数据集创建峰值,以使曲线平滑。我希望使用 geom_smooth()。我也希望线根据观察的顺序进行(而不是根据x轴)。当我使用 geom_path()时,它适用于原始图,但不适用于结果 geom_smooth()(请参阅

  melteddf = Storfjorden%>%melt(id.vars =深度)
ggplot (),
scale_y_reverse()+
geom_smooth(span,x = value)+
facet_wrap(〜variable,nrow = 1,scales =free_x = 0.5,se = FALSE)+
geom_path()


因此,有没有办法确保平滑曲线按照观察顺序而不是a轴顺序进展?


我的数据的子集:

$头部(Storfjorden)
深度盐度温度荧光
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


解决方案

您提供的数据非常少,但我们可以使其成功。

使用一些tidyverse软件包,我们可以为每个变量分别配置黄土函数。



我们的工作本质上是
$ b $ ol

  • variable group_by )。

  • 使用 code>来适应每个组的黄土功能。
  • 使用 augment 从该黄土模型创建预测,在此在数据范围内(对于变量),有1000个值。


     #加载软件包
    library(dplyr)
    library(broom)

    lo< - melteddf%>%
    group_by(变量)%>%
    do(增加(黄色(值〜深度,数据=。),
    newdata = data.frame(Depth = seq(min(。$ Depth),max(。$ Depth),l = 1000))))

    现在我们可以在新的 geom_path 调用中使用该预测数据:

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

    解决方案

    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:

    这篇关于ggplot2:geom_smooth select观察连接(与geom_path()等价)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆