【问题标题】:ggplot2 multiline spline smoothingggplot2多线样条平滑
【发布时间】:2022-01-17 03:05:54
【问题描述】:

如何在下面的代码段中对多线图进行样条平滑?附图显示了这段代码生成的两个图。

谢谢!

library(tidyverse)
library(ggplot2)

# group by cyl
by_cyl <- mtcars %>% group_by(cyl) %>% summarize(mpg = mean(mpg))

# group by cyl & gear
by_cyl_gear <- mtcars %>% group_by(cyl, gear) %>% summarize(mpg = mean(mpg))

# spline smoothing for a single line plot
spline_d <- as.data.frame(spline(by_cyl$cyl, by_cyl$mpg))
ggplot(data = by_cyl, aes(x = cyl, y = mpg)) +
    geom_line(data = spline_d, aes(x = x, y = y)) +
    geom_point() +
    ggtitle("Unpartitioned (smooth)")

# multi-line plot - how to do spline-smoothing here?
by_cyl_gear$gear <- as.character(by_cyl_gear$gear)
ggplot(data = by_cyl_gear, aes(x = cyl, y = mpg)) +
    geom_line(aes(color = gear)) +
    geom_point(aes(color = gear)) +
    ggtitle("Partitioned by gear (non-smooth)") +
    theme(legend.position = c(0.9, 0.6))

【问题讨论】:

    标签: r ggplot2 spline


    【解决方案1】:

    基本上它与单行相同。你可以splitgear,使用lapply计算每个分割的样条,然后使用例如绑定在一起。 bind_rows:

    library(dplyr)
    library(ggplot2)
    
    # group by cyl & gear
    by_cyl_gear <- mtcars %>% 
      group_by(cyl, gear) %>% 
      summarize(mpg = mean(mpg)) %>% 
      mutate(gear = as.character(gear))
    #> `summarise()` has grouped output by 'cyl'. You can override using the `.groups` argument.
    
    # split, lapply, bind_rows
    spline_d <- by_cyl_gear %>% 
      split(.$gear) %>% 
      lapply(function(x) as.data.frame(spline(x$cyl, x$mpg))) %>% 
      bind_rows(.id = "gear")
    
    ggplot(data = by_cyl_gear, aes(x = cyl, y = mpg, color = gear)) +
      geom_line(data = spline_d, aes(x = x, y = y)) +
      geom_point() +
      ggtitle("Partitioned by gear (non-smooth)") +
      theme(legend.position = c(0.9, 0.6))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多