如何用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



  • 10
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要将echarts折线图改成散点图,你可以按照以下步骤进行修改: 1. 首先,将代码示例中的option对象中的type属性从'line'改为'scatter',以指定图表类型为散点图。 修改后的代码示例如下: ``` series: \[ { name:'曲线1', symbolSize: 5, data: \[ \[0, 2\], \[2, 5\], \[3, 9\], \[5, 16\], \[7, 24\], \[9, 18\], \[12, 15\], \[15, 5\], \[17, 2\], \[19, 1\] \], type: 'scatter', smooth: true, showSymbol: false, barGap: 0 }, { name:'曲线2', symbolSize: 5, data: \[ \[0, 1\], \[2, 3\], \[3, 7\], \[5, 13\], \[7, 21\], \[9, 16\], \[12, 12\], \[15, 3\], \[17, 1\], \[19, 0\] \], type: 'scatter', smooth: true, showSymbol: false, barGap: 0 } \] ``` 2. 然后,根据你的需求,可以进一步调整散点图的其他配置项,比如symbolSize属性可以调整散点的大小,smooth属性可以控制是否平滑显示散点图等。 完成以上修改后,你的echarts折线图就成功改成了散点图。 #### 引用[.reference_title] - *1* *3* [echarts图表-饼图、柱状图、折线图散点图之间相互切换](https://blog.csdn.net/m0_72603435/article/details/127970766)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [echarts-散点图数值对实现折线图](https://blog.csdn.net/fightingcoder419/article/details/79817562)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值