如何用R画折线图,散点图,平滑曲线图

R语言绘图教程
本文详细介绍了如何使用R语言绘制折线图、散点图和平滑曲线图,并提供了具体的数据和代码实例,帮助读者快速掌握不同图表类型的绘制方法。

如何用R画折线图,散点图,平滑曲线图

例子:

week

1

2

3

4

5

6

x

3

8

19

24

6

1

y

1

25

21

3

2

1

要求是以week为横坐标,画出x-weeky-week的折线图,散点图及平滑曲线图。

一.散点图

## 输入数据 ##

> week<-c(1,2,3,4,5,6)

> x<-c(3,8,19,24,6,1)

> y<-c(1,25,21,3,2,1)

## x-week散点图 ##

plot(week,x,col="red",pch=22,bg="yellow",xlim=c(0,6),ylim=c(0,30),lwd=2,xlab="WEEK",ylab="STUDENT",main="lesson",sub="class",col.main="green",font.main=2,asp=0,cex=1.2)
 
## 结果如下 ##

如何用R画折线图,散点图,平滑曲线图

## 注释 ##

col 颜色

pch symbol 属性 1-25

   如何用R画折线图,散点图,平滑曲线图如何用R画折线图,散点图,平滑曲线图

xlimylim为坐标刻度范围

xlabylab为坐标标题

lwd  line width for drawing symbols

main 为图标题

sub 为图下标题

col.main 为设置图标题颜色

font.main 为设置图标题字体

cel symble 大小

lty 为折线类型 1为实线, 2为虚线

 

二、折线图

## 折线图只需在plot函数中加入type=“ ” ##

plot(week,x,col="red",pch=22,bg="yellow",xlim=c(0,6),ylim=c(0,30),lwd=2,xlab="WEEK",ylab="STUDENT",main="lesson",sub="class",col.main="green",font.main=2,asp=0,cex=1.2,type="b",lty=1)

如何用R画折线图,散点图,平滑曲线图

 

type

what type of plot should be drawn. Possible types are

·         "p" for points,

·         "l" for lines,

·         "b" for both,

·         "c" for the lines part alone of "b",

·         "o" for both ‘overplotted’,

·         "h" for ‘histogram’ like (or ‘high-density’) vertical lines,

·         "s" for stair steps,

·         "S" for other steps, see ‘Details’ below,

·         "n" for no plotting.

 

## 添加一条线 使用函数lines()##

>lines(week,y,col="green",pch=16,bg="yellow",xlim=c(0,6),ylim=c(0,30),lwd=2,xlab="WEEK",ylab="STUDENT",main="lesson",sub="class",col.main="green",font.main=2,asp=0,cex=1.2,type="b",lty=2)

如何用R画折线图,散点图,平滑曲线图

## 对图添加栅格 使用grid()函数 ##

grid(nx=6,ny=6,lwd=2)

如何用R画折线图,散点图,平滑曲线图

如果只需横轴上有栅格,则ny=NA

 

## 如何在图中添加一条直线 abline() 函数##

> abline(h=18,col="black",lty=1,lwd=2)

如何用R画折线图,散点图,平滑曲线图

## 如何添加图例 legend() 函数##

>legend("topright",legend=c("x","y"),pch=c(22,16),col=c("red","green"),lwd=2,lty=c(1,2))

如何用R画折线图,散点图,平滑曲线图

当然topright 还可以用坐标位置代替(xy

 

 平滑曲线图

## 使用函数 spline(x,y,n=?) ##

> sp=spline(week,x,n=1000)

> sp1=spline(week,y,n=1000)

n值表示平滑程度

> plot(sp,col="red",type="l",xlim=c(0,6),ylim=c(0,30),lwd=2,xlab="WEEK",ylab="STUDENT",main="lesson",sub="class",col.main="green",font.main=2)

> lines(sp1,col="green",type="l",xlim=c(0,6),ylim=c(0,30),lwd=2,xlab="WEEK",ylab="STUDENT",main="lesson",sub="class",col.main="green",font.main=2)

> legend("topright",legend=c("x","y"),col=c("red","green"),lwd=2,lty=c(1,2))

如何用R画折线图,散点图,平滑曲线图




转载自:http://blog.sina.com.cn/s/blog_5d188bc40102vu2b.html



在C#中,可使用Chaikin算法实现散点图曲线平滑。Chaikin算法通过控制张力因子和迭代次数来调整曲线平滑程度和精度,对原始点集合进行切割和插值操作,进而得到平滑曲线坐标点集合,该算法能有效平滑折线,且具有较高的精度和可控性[^1]。 以下是一个简单示例代码,展示如何使用Chaikin算法: ```csharp using System; using System.Collections.Generic; // 定义一个表示二维点的结构体 public struct Point2D { public double X; public double Y; public Point2D(double x, double y) { X = x; Y = y; } } // 实现Chaikin算法的类 public class ChaikinSmoothing { // 执行Chaikin平滑算法的方法 public static List<Point2D> Smooth(List<Point2D> points, int iterations) { List<Point2D> smoothedPoints = new List<Point2D>(points); // 进行指定次数的迭代 for (int i = 0; i < iterations; i++) { smoothedPoints = SmoothOnce(smoothedPoints); } return smoothedPoints; } // 单次迭代的平滑操作 private static List<Point2D> SmoothOnce(List<Point2D> points) { List<Point2D> newPoints = new List<Point2D>(); // 遍历原始点列表,除了最后一个点 for (int i = 0; i < points.Count - 1; i++) { // 计算第一个新点 Point2D p1 = new Point2D( 0.75 * points[i].X + 0.25 * points[i + 1].X, 0.75 * points[i].Y + 0.25 * points[i + 1].Y ); // 计算第二个新点 Point2D p2 = new Point2D( 0.25 * points[i].X + 0.75 * points[i + 1].X, 0.25 * points[i].Y + 0.75 * points[i + 1].Y ); // 将新点添加到结果列表中 newPoints.Add(p1); newPoints.Add(p2); } return newPoints; } } ``` ### 使用示例 ```csharp class Program { static void Main() { // 定义原始的散点 List<Point2D> originalPoints = new List<Point2D> { new Point2D(0, 0), new Point2D(1, 2), new Point2D(2, 1), new Point2D(3, 3) }; // 进行平滑处理,迭代2次 List<Point2D> smoothedPoints = ChaikinSmoothing.Smooth(originalPoints, 2); // 输出平滑后的点 foreach (var point in smoothedPoints) { Console.WriteLine($"({point.X}, {point.Y})"); } } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值