R语言,同一张图中绘制多个折线 multi-line plot

这个事情比较简单,就是单纯记录一下过程,因为我好久没写R代码了,很多东西都忘了。

1. 先看一下数据, 就4列。:

> head(arbuthnot)   
  year boys girls total
1 1629 5218  4683  9901
2 1630 4858  4457  9315
3 1631 4422  4102  8524
4 1632 4994  4590  9584
5 1633 5158  4839  9997
6 1634 5035  4820  9855

2. 代码:

#读取文件。

# R语言中索引是从1开始的
# 从网页下载并加载R脚本
# source 命令在R语言中用于执行一个字符串或文件中的R代码。
# 它不仅可以读取网络上的文件,也可以读取本地文件。
source("http://www.openintro.org/stat/data/arbuthnot.R")

head(arbuthnot)  # 1629 
tail(arbuthnot)  # 1705

class(arbuthnot)
 
arbuthnot$total = arbuthnot$girls + arbuthnot$boys

arbuthnot

plot(x = arbuthnot$year, y = arbuthnot$total, type="n",
     xlim = c(1629, 1710), ylim = c(1, 17000),
     xlab ="Year", ylab="count",
     main="Plot boys, girls and total on the same figure.")


lines(x = arbuthnot$year, y = arbuthnot$boys,
      type = "l", col = "red")

lines(x = arbuthnot$year, y = arbuthnot$girls,
      type = "l", col = "blue")

lines(x = arbuthnot$year, y = arbuthnot$total,
      type = "l", col = "green")


# lty  line type
# lty = 1 表示实线(solid line),这是默认的线型。
legend("topleft", 
       legend = c("boys", "girls", "total"), 
       col = c("red", "blue", "green"), lty = 1)

3. 绘图效果:

4. 其他:

R语言代码,一般比较简短,而且大多数都是单文件,直接复制到 ai 搜索框,然后每一行自动添加注释,便于理解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值