利用 R 绘制拟合曲线

Table of Contents

  1. 利用 R 绘制拟合曲线
    1. 单纯使用 goemsmooth
    2. 单纯使用 spline
    3. 同时使用前两种方法

利用 R 绘制拟合曲线

我的主要使用 ggplot2 进行绘图,这里也只介绍 ggplot2 的相关方法。ide

利用 R 绘制拟合曲线主要有两类方法:spa

  1. 利用 geomsmooth 进行曲线的拟合(method 选择 loess 或者添加 formula);
  2. 利用 spline 进行插值操做,而后用 geomline 进行链接。

可是,这两种方法都有一些缺陷。利用 geomsmooth 进行曲线的拟合在某些数据的状况下会拟合比较差,甚至呈现折线。利用 spline 进行插值操做后绘图会致使曲线必须通过实际值对应的点,致使曲线僵硬。在某些状况下,两种方法都没法获得咱们须要的图形。3d

在本文中,须要绘制以下数据的图形:code

density ROS
0 3.43
0.001 1.86
0.01 56.00
0.1 225.31
1 183.56
10 339.40
100 272.89
1000 204.17
10000 2.29

该数据表现了样品随着浓度的变化,观测值变化的状况。orm

单纯使用 goem_smooth

library(ggplot2)
    ros <- read.csv('tem.csv', colClasses = c('character', 'numeric'))
    ggplot(ros, aes(x=density, y=ROS)) +
      geom_point() +
      geom_smooth(aes(x=density2, y=ROS), se = F, method = 'loess')

单纯使用 spline

ros <- transform.data.frame(ros, density2=1:9)
    tem <- as.data.frame(spline(ros$density2, ros$ROS, n=10000))
    ggplot(ros, aes(x=density, y=ROS)) +
      geom_point() +
      geom_line(data = tem, aes(x=x, y=y))

同时使用前两种方法

tem2 <- as.data.frame(spline(ros$density2, ros$ROS, n=100))
    ggplot(ros, aes(x=density, y=ROS)) +
      geom_point() +
      geom_smooth(data = tem2, aes(x=x, y=y), se = F, method = 'loess')

相关文章
相关标签/搜索