R基础绘图包之plot及参数

这篇文章可以让你学到什么?

可以让你把这样的图:

Rplot01.png

通过设置各种参数修改为这样自定义的图:

Rplot2.png

一、 本文使用的数据

我们使用R内置的数据集Chickweight来进行画图的学习,首先了解一下数据的大致情况

  1. 查看帮助文档

因为Chickweight是R内置数据集,可以查看帮助文档来了解数据

? ChickWeight

从帮助文档中可以看到这个数据集是一个578行,4列的数据框,是刚出生的小鸡吃四种不同食物体重变化情况的数据,每个变量的含义分别是:

weight: 体重,数值型数据
Time: 时间,小鸡的日龄,数值型数据
Chick: 小鸡的编号,因子型数据
Diet: 食物的编号,因子型数据

  1. 使用基础函数了解数据
# 数据赋值给mydata
mydata <- ChickWeight
# 查看前几行
head(mydata)
# 查看数据的类型
class(mydata)
# 了解数据的底细
str(mydata)
# 了解数据的概况
summary(mydata)

二、 画图函数及参数的学习

Step1 使用默认参数画出图形

1.1 画出1号小鸡体重随着时间变化的情况

# 判断第三列Chick等于1,然后用中括号提取出仅仅包含1号小鸡的数据,命名为chick1
chick1 <- mydata[mydata$Chick == 1, ]

# 以chick1的日龄为横坐标,体重为纵坐标,不设置任何参数,默认画出散点图
plot(x = chick1$Time, y = chick1$weight)

Step2 修改图形主体部分

2.1 修改图形的类型

我不喜欢这个只有点的图,想要修改一下,怎么做呢?

  • 参数

增加一个type参数,即可修改图形的类型了

# 设置type = "b",画出既有点又有线的图
plot(x = chick1$Time, y = chick1$weight, type = "b")
  • 有哪些类型可以选择

type = "p" :点图
type = "l" :折线图,
type = "b" :既有点图又有折线,就叫它附点折线图吧
type = "c" :只画附点折线图线的那部分
type = "o" :重叠的附点折线图
type = "h" :垂直线图
type = "s" :阶梯图
type = "S" :另外一种阶梯图
type = "n" :不画图,只画出坐标

4.png

2.2 修改图形元素大小

我选择了既有点有又有线的图,现在我想要把点调大点,线调粗一点,应该怎么做呢?

  • 参数

cex : 修改点图中点的大小
lwd : 修改线图中线的粗细

plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5)
  • 有哪些大小可以选择?

cex : 默认大小的任意倍数
lwd : 默认粗细的任意倍数

6.png

2.3 修改图形元素的形状

我想把空心圆和实线修改成实心圆点和虚线要怎么做?

  • 参数

pch: 修改点的款式
lty :修改线条的款式

plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,
     pch = 16, lty = 2)
  • 有哪些点和线的款式可以选择?

pch:取值范围1-25
lty :取值范围1-6

pch取值类型:

8.png

lty取值类型:

9.png

2.4 修改图形元素的颜色

  • 参数

col :设置颜色


plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,
     pch = 16, lty = 2,
     col = "blue")

  • 有哪些颜色可以选择?

col: 取值可以是颜色名称如"blue",RGB值如rgb(0,0,1),颜色代码如"#0000FF"
运行colours()可以获取颜色的名称,总共有657种

head(colours())
length(colours())

运行rainbow(n)可以获得n种彩虹色


mycolor <- rainbow(7)
plot(1:7, rep(5,times = 7), type = "h", lwd = 25,
     col= mycolor)

11.png

我比较喜欢上面的第5种颜色,我就可以用mycolor[5]取出这个颜色运用到我的图里:

mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,
     pch = 16, lty = 2,
     col = mycolor[5])

Step3 修改图形标题部分

我们会发现plot()的函数给x轴和y轴标题默认设置为变量的名称,但是我们不喜欢,想要修改,并且想给整个图形加上大标题,应该怎么做呢?

3.1 设置图形标题

  • 参数

main:设置主标题
sub:设置次标题
xlab:设置x轴标题
ylab:设置y轴标题

mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight")

3.2 修改文字大小

  • 参数

cex.axis : 坐标轴刻度文字大小
cex.lab: 坐标轴标题文字大小
cex.main : 主标题文字大小
cex.sub :次标题文字大小
以上参数均与cex参数类似,设置默认大小的倍数即可相应放大或缩小

mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2)
3.3 修改文字字体
  • 参数

font.axis : 设置坐标刻度字体,1常规字体,2加粗字体,3斜体字,4斜体加粗
font.lab:设置坐标轴标题字体,取值同font.axis
font.main :设置主标题字体,取值同font.axis
font.sub :设置次标题字体,取值同font.axis
family:设置字体样式系列,family = 'serif',family = 'sans',family = 'mono'

mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif")

3.4 修改标题文字颜色

  • 参数

col.axis: 设置坐标刻度字体颜色
col.lab: 设置坐标轴标题字体颜色
col.main : 设置主标题字体颜色
col.lab : 设置次标题字体颜色

mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.main = mycolor[6], col.sub = mycolor[5])

3.5 添加标题和禁用标题

  • 参数

ann = FALSE : 设置之后即可不显示原标题

mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.main = mycolor[6], col.sub = mycolor[5],
     ann = FALSE)
  • 函数

title() : 使用title()函数可以为图形另外设置标题,其参数与plot()函数中设置标题的参数相同

mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.main = mycolor[6], col.sub = mycolor[5],
     ann = FALSE)

title(main = "Chick1`s weight", sub = "From birth to 21 days",
      xlab = "Time(day)",
      col.main = "red", col.sub = "blue",
      col.lab = "green",
      cex.axis = 0.8, cex.lab = 1.5, cex.main = 2, cex.sub = 1.5,
      font.axis = 3, font.lab = 4, font.main = 2, font.sub = 2)

# 使用title()函数就可以将x和y轴的标题设置为不同的颜色了!
title(ylab = "Weight",
      col.lab = "orange",
      cex.lab = 1.5, font.lab = 3)
3.5.png

Step4 修改图形坐标轴与边框

4.1 隐藏框架线及坐标轴

  • 参数

fram.plot = FALSE : 隐藏框架线
xaxt = "n" : 隐藏X轴,会留下框架
yaxt = "n" : 隐藏Y轴,会留下框架
axes=FALSE:隐藏所有坐标轴及框架线

隐藏框架线:

mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.main = mycolor[6], col.sub = mycolor[5],
     frame.plot = FALSE)

隐藏框架线及坐标轴运行结果展示:

opar <- par(no.readonly = TRUE)
par(mfrow=c(2,2))

# fram.plot = FALSE 
mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "fram.plot = FALSE ", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.sub = mycolor[5],
     frame.plot = FALSE)

# fram.plot = FALSE ,xaxt = "n"
mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "fram.plot = FALSE ,xaxt = 'n'", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.sub = mycolor[5],
     frame.plot = FALSE, xaxt = "n")

# fram.plot = TRUE ,yaxt = "n"
mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "fram.plot = TRUE,yaxt = 'n'", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.sub = mycolor[5],
     frame.plot = TRUE, yaxt = "n")

# axes = FALSE
mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "axes = FALSE", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.sub = mycolor[5],
     axes = FALSE)

par(opar)

4.2 设置坐标轴取值范围

  • 参数

xlim: 设置x轴取值范围
ylim: 设置y轴取值范围

mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.main = mycolor[6], col.sub = mycolor[5],
     frame.plot = FALSE,
     xlim = c(0,25), ylim = c(0, 250))

4.3 自定义坐标轴

  • 函数

axis() : 例如axis(1, at = 0:21, labels = as.character(0:21), col = "red", las = 2)

  • 参数

1: 设置x轴
2: 设置y轴
at : 设置刻度所在位置的,数值向量
labels : 设置刻度的标签,字符向量
col : 设置坐标轴的颜色
las : 设置刻度标签字体方向,水平或者垂直,取值0,1,2
col.ticks : 刻度的颜色
col.axis : 刻度标签的颜色

mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.main = mycolor[6], col.sub = mycolor[5],
     frame.plot = FALSE,
     xlim = c(0,25), ylim = c(0, 250),
     axes = FALSE)

axis(1, at = 0:21, labels = as.character(0:21), col = "blue",
     col.ticks = "red",las = 2)
axis(2, at = seq(0, 250, 50), labels = as.character(seq(0, 250, 50)), col = "red", las = 0, col.ticks = "blue", col.axis = "green")

4.4 添加次要刻度线

  • R包

Hmisc包 :首次使用需要安装install.packages("Hmisc")

  • 函数

minor.tick(): 格式minor.tick(nx = 2, ny = 5, tick.ratio = 0.5, x.args = list(col = "red"),y.args = list(col = "red"))

  • 参数

nx : x轴加入次要刻度线之后把原来的每个分成多少格
ny : y轴加入次要刻度线之后把原来的每个分成多少格
tick.ratio : 次要刻度线相对主要刻度线的大小
x.args : 用来设置x轴的其他参数的列表,参数同axis(), 比如col等
y.args : 用来设置y轴的其他参数的列表,参数同axis(), 比如col等

mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.main = mycolor[6], col.sub = mycolor[5],
     frame.plot = FALSE,
     xlim = c(0,25), ylim = c(0, 250),
     axes = FALSE)

axis(1, at = 0:21, labels = as.character(0:21), col = "blue",
     col.ticks = "red",las = 2)
axis(2, at = seq(0, 250, 50), labels = as.character(seq(0, 250, 50)), col = "red", las = 0, col.ticks = "blue", col.axis = "green")
library(Hmisc)
minor.tick(nx = 2, ny = 5, tick.ratio = 0.5, x.args = list(col = "green"),y.args = list(col = "orange"))
4.4.png

Step5 修改图形整体尺寸

5.1 修改图形整体尺寸大小

  • 函数

par()

  • 参数

no.readonly = TRUE : 设置画板的参数为可修改状态
pin: 以英寸表示的图形尺寸(宽和高)
mai: 以数值向量表示的边界大小,顺序为“下、左、上、右”,单位为英寸
mar: 以数值向量表示的边界大小,顺序为“下、左、上、右”,单位为英分。默认(5,4,4,2)+ 0.1

opar <- par(no.readonly = TRUE)
par(pin = c(3,4), mar = c(5,8,2,6))
mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.main = mycolor[6], col.sub = mycolor[5],
     frame.plot = FALSE,
     xlim = c(0,25), ylim = c(0, 250),
     axes = FALSE)

axis(1, at = 0:21, labels = as.character(0:21), col = "blue",
     col.ticks = "red",las = 2)
axis(2, at = seq(0, 250, 50), labels = as.character(seq(0, 250, 50)), col = "red", las = 0, col.ticks = "blue", col.axis = "green")

minor.tick(nx = 2, ny = 5, tick.ratio = 0.5, x.args = list(col = "green"),y.args = list(col = "orange"))

par(opar)
5.1.png

Step6 给图形添加参考线、图例、文字

6.1 添加参考线

  • 函数

abline()

  • 参数

v : 垂直参考线的位点
h : 水平参考线的位点
lty: 参考线的样式
col:参考线的颜色

opar <- par(no.readonly = TRUE)
par(pin = c(3,4), mar = c(5,8,2,6))
mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.main = mycolor[6], col.sub = mycolor[5],
     frame.plot = FALSE,
     xlim = c(0,25), ylim = c(0, 250),
     axes = FALSE)

axis(1, at = 0:21, labels = as.character(0:21), col = "blue",
     col.ticks = "red",las = 2)
axis(2, at = seq(0, 250, 50), labels = as.character(seq(0, 250, 50)), col = "red", las = 0, col.ticks = "blue", col.axis = "green")

minor.tick(nx = 2, ny = 5, tick.ratio = 0.5, x.args = list(col = "green"),y.args = list(col = "orange"))
abline(v = seq(5,25,5), h = seq(50,250,50), lty = 3, col = "blue")
par(opar)

6.2 添加图例

  • 函数

legend()
legend("right", inset = 0.02, title = "Chick", c("Chick1"), lty = c(2), pch = c(16), col = c("blue"))

  • 参数

"right" : 图例的位置,右方,也可以设置为其他位置,请?legend
inset : 图例向图形内部偏移的比率
title: 图例的名称
c("Chick1"), : 图列的标签组成的向量
lty : 图例的线条类型组成的向量
pch : 图例的点的类型组成的向量
col: 图列的颜色组成的向量

opar <- par(no.readonly = TRUE)
par(pin = c(3,4), mar = c(5,8,2,6))
mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.main = mycolor[6], col.sub = mycolor[5],
     frame.plot = FALSE,
     xlim = c(0,25), ylim = c(0, 250),
     axes = FALSE)

axis(1, at = 0:21, labels = as.character(0:21), col = "blue",
     col.ticks = "red",las = 2)
axis(2, at = seq(0, 250, 50), labels = as.character(seq(0, 250, 50)), col = "red", las = 0, col.ticks = "blue", col.axis = "green")

minor.tick(nx = 2, ny = 5, tick.ratio = 0.5, x.args = list(col = "green"),y.args = list(col = "orange"))
abline(v = seq(5,25,5), h = seq(50,250,50), lty = 3, col = "blue")

legend("right", inset = 0.02, title = "Chick", c("Chick1"),
       lty = c(2), pch = c(16), col = c(mycolor[5]))
par(opar)

6.3 添加文本标注

  • 函数

text()向绘图区域内部添加文本
text(x, y , labels , pos , cex , col )

mtext()向图形四个边界之一添加文本
mtext("Chick1-diet1", side = 4,line = -6,las = 2)

  • 参数

text()
x: 标注位点的横坐标
y : 标注位点的纵坐标
labels: 所要添加的标注内容
pos: 标注所在相对位置,1,2,3,4 分别代表下,左,上,右
cex: 字体大小
col: 字体颜色

metxt()
labels : 所要添加的标注内容
side : 1,2,3,4分别代表下,左,上,右四个边界
line : 相对边界线的偏移距离
las : 标注文字的方向,0,1,垂直或水平

opar <- par(no.readonly = TRUE)
par(pin = c(3,4), mar = c(5,8,2,6))
mycolor <- rainbow(7)
plot(x = chick1$Time, y = chick1$weight, type = "b",
     cex = 1.5, lwd = 1.5,pch = 16, lty = 2,col = mycolor[5],
     main = "Chick1`s Weight", sub = "From birth to 21 days",
     xlab = "Time(day)", ylab = "Weight",
     cex.axis = 0.8, cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.axis = 3, font.lab = 1, font.main = 2, font.sub =4,
     family = "serif",
     col.axis = mycolor[7], col.lab = mycolor[1],
     col.main = mycolor[6], col.sub = mycolor[5],
     frame.plot = FALSE,
     xlim = c(0,25), ylim = c(0, 250),
     axes = FALSE)

axis(1, at = 0:21, labels = as.character(0:21), col = "blue",
     col.ticks = "red",las = 2)
axis(2, at = seq(0, 250, 50), labels = as.character(seq(0, 250, 50)), col = "red", las = 0, col.ticks = "blue", col.axis = "green")

minor.tick(nx = 2, ny = 5, tick.ratio = 0.5, x.args = list(col = "green"),y.args = list(col = "orange"))
abline(v = seq(5,25,5), h = seq(50,250,50), lty = 3, col = "blue")

legend("right", inset = 0.02, title = "Chick", c("Chick1"),
       lty = c(2), pch = c(16), col = c(mycolor[5]))

text(chick1$Time, chick1$weight,as.character(chick1$weight), cex = 0.6, pos = 3, col = "red")

mtext("Chick1-diet1", side = 2,line = -6,las = 1, col = "red")

par(opar)
6.3.png

Step7 组合多个图形

上面我们画出了1号小鸡的体重变化图,现在我们想要画出更多小鸡的体重变化图形,并且把它们组合到一起对比,应该怎么办呢?

7.1 使用pai()中的mfrowmfcol参数组合图形

  • 先分别提取出各个小鸡的数据来:
chick1 <- mydata[mydata$Chick == 1, ]
chick2 <- mydata[mydata$Chick == 2, ]
chick3 <- mydata[mydata$Chick == 3, ]
chick4 <- mydata[mydata$Chick == 4, ]
chick5 <- mydata[mydata$Chick == 5, ]
chick6 <- mydata[mydata$Chick == 6, ]
  • mfrow=(2,3) 设置按行填充图形为2行3列
opar <- par(no.readonly = TRUE)
par(mfrow = c(2,3))
plot(chick1$Time, chick1$weight)
plot(chick2$Time, chick2$weight)
plot(chick3$Time, chick3$weight)
plot(chick4$Time, chick4$weight)
plot(chick5$Time, chick5$weight)
plot(chick6$Time, chick6$weight)
par(opar)
  • mfcol=c(3,2) 设置按列填充图形为3行2列
opar <- par(no.readonly = TRUE)
par(mfcol= c(3,2))
plot(chick1$Time, chick1$weight)
plot(chick2$Time, chick2$weight)
plot(chick3$Time, chick3$weight)
plot(chick4$Time, chick4$weight)
plot(chick5$Time, chick5$weight)
plot(chick6$Time, chick6$weight)
par(opar)

7.2 使用函数layout()组合图形

  • 参数

mat : 一个矩阵,设置图形的总体布局
widths: 向量,设置图形矩阵中每列相对宽度
heights: 向量,设置图形矩阵中每行的相对高度

运行以下代码,即可理解layout()的布局规律:

mat <- matrix(c(1,2,3,4,5,6), 2, 3, byrow = TRUE)
mat
layout(mat, widths = c(1,1,2), heights = c(1,2))
plot(chick1$Time, chick1$weight)
plot(chick2$Time, chick2$weight)
plot(chick3$Time, chick3$weight)
plot(chick4$Time, chick4$weight)
plot(chick5$Time, chick5$weight)
plot(chick6$Time, chick6$weight)
mat <- matrix(c(1,2,3,4,5,5,6,6,6), 3, 3, byrow = TRUE)
mat
layout(mat)
plot(chick1$Time, chick1$weight)
plot(chick2$Time, chick2$weight)
plot(chick3$Time, chick3$weight)
plot(chick4$Time, chick4$weight)
plot(chick5$Time, chick5$weight)
plot(chick6$Time, chick6$weight)
mat <- matrix(c(1,2,3,4,5,5,6,6,6), 3, 3, byrow = TRUE)
mat
layout(mat,widths = c(1,2,1), heights = c(2,2,2))
plot(chick1$Time, chick1$weight)
plot(chick2$Time, chick2$weight)
plot(chick3$Time, chick3$weight)
plot(chick4$Time, chick4$weight)
plot(chick5$Time, chick5$weight)
plot(chick6$Time, chick6$weight)

7.3 par()fig参数设置图形位置,叠加图形

  • 参数

fig
整个画布大可以理解为fig = c(0,1,0,1), 向量中的4个数依次代表:x轴起始位置0,x轴结束位置1,y轴起始位置0,y轴结束位置1
把图形摆放在画面由半部分,设置fig = c(0.5,1,0,1)
把图放在右上四分之一的位置,设置fig = c(0.5,1,0.5,1)

new = TRUE : 在原有图形增加新图形,需要设置 par(new = TRUE)

opar <- par(no.readonly = TRUE)
par(fig = c(0,0.5,0.5,1))
plot(chick1$Time, chick1$weight)

par(fig = c(0.5,1,0.5,1), new = TRUE)
plot(chick2$Time, chick2$weight)

par(fig = c(0,0.5,0,0.5), new = TRUE)
plot(chick3$Time, chick3$weight)

par(fig = c(0.5,1,0,0.5), new = TRUE)
plot(chick4$Time, chick4$weight)
par(opar)
7.3.png

三、 实战演练

需求

画出食用不同饲料的小鸡,平均体重随着时间变化的趋势图,并且组合到一张图片中对比

数据处理

# 提取数据
mydata <- ChickWeight
# 把变量名称全部改成小写
names(mydata) <- tolower(names(mydata))

# 将原始的宽型数据转换为长型数据
library(reshape2)
mydatam <- melt(mydata, id = 2:4, na.rm = TRUE)

# 使用dcast函数,将数据整理成吃不同食物小鸡每天的平均体重
dmean <- dcast(mydatam , time ~ diet, mean)

画图

# 设置颜色和布局
mycolor <- rainbow(7)
opar <- par(no.readonly = TRUE)
par(mfrow = c(2,2))

## diet 1

n <- 1
y <- dmean[,n+1]
plot <- plot(x = dmean$time, y, type = "b",
     cex = 0.8, lwd = 1.2,pch = 16, lty = 2,col = mycolor[5],
     main = paste("Diet",n),
     xlab = "", ylab = "",
     cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
     font.lab = 1, font.main = 4,
     family = "serif",
     col.lab = mycolor[5],col.main ="blue",
     frame.plot = FALSE,
     xlim = c(0,22), ylim = c(0, 280),
     axes = FALSE)
xx <- axis(1, at = seq(0,22,2), labels = as.character(seq(0,22,2)), 
     col = "blue",col.ticks = "blue",col.axis = "blue",
     las = 1,font.axis = 3)
yy <- axis(2, at = seq(0, 280, 50), labels = as.character(seq(0, 280, 50)),
     col = "blue", col.ticks = "blue", col.axis = "blue",
     las = 1,font.axis = 3)

mit <- minor.tick(nx = 5, ny = 5, tick.ratio = 0.5, 
           x.args = list(col = "blue"),
           y.args = list(col = "blue"))
abl <- abline(v = seq(5,25,5), h = seq(50,280,50), lty = 3, col = "orange")
txt <- text(dmean$time, y,as.character(round(y,0)), cex = 0.6, pos = 3, col = "red")

# diet 2
n <- 2
y <- dmean[,n+1]
plot <- plot(x = dmean$time, y, type = "b",
             cex = 0.8, lwd = 1.2,pch = 16, lty = 2,col = mycolor[5],
             main = paste("Diet",n),
             xlab = "", ylab = "",
             cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
             font.lab = 1, font.main = 4,
             family = "serif",
             col.lab = mycolor[5],col.main ="blue",
             frame.plot = FALSE,
             xlim = c(0,22), ylim = c(0, 280),
             axes = FALSE)
xx <- axis(1, at = seq(0,22,2), labels = as.character(seq(0,22,2)), 
           col = "blue",col.ticks = "blue",col.axis = "blue",
           las = 1,font.axis = 3)
yy <- axis(2, at = seq(0, 280, 50), labels = as.character(seq(0, 280, 50)),
           col = "blue", col.ticks = "blue", col.axis = "blue",
           las = 1,font.axis = 3)

mit <- minor.tick(nx = 5, ny = 5, tick.ratio = 0.5, 
                  x.args = list(col = "blue"),
                  y.args = list(col = "blue"))
abl <- abline(v = seq(5,25,5), h = seq(50,280,50), lty = 3, col = "orange")
txt <- text(dmean$time, y,as.character(round(y,0)), cex = 0.6, pos = 3, col = "red")

# diet 3
n <- 3
y <- dmean[,n+1]
plot <- plot(x = dmean$time, y, type = "b",
             cex = 0.8, lwd = 1.2,pch = 16, lty = 2,col = mycolor[5],
             main = paste("Diet",n),
             xlab = "", ylab = "",
             cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
             font.lab = 1, font.main = 4,
             family = "serif",
             col.lab = mycolor[5],col.main ="blue",
             frame.plot = FALSE,
             xlim = c(0,22), ylim = c(0, 280),
             axes = FALSE)
xx <- axis(1, at = seq(0,22,2), labels = as.character(seq(0,22,2)), 
           col = "blue",col.ticks = "blue",col.axis = "blue",
           las = 1,font.axis = 3)
yy <- axis(2, at = seq(0, 280, 50), labels = as.character(seq(0, 280, 50)),
           col = "blue", col.ticks = "blue", col.axis = "blue",
           las = 1,font.axis = 3)

mit <- minor.tick(nx = 5, ny = 5, tick.ratio = 0.5, 
                  x.args = list(col = "blue"),
                  y.args = list(col = "blue"))
abl <- abline(v = seq(5,25,5), h = seq(50,280,50), lty = 3, col = "orange")
txt <- text(dmean$time, y,as.character(round(y,0)), cex = 0.6, pos = 3, col = "red")

# diet 4
n <- 4
y <- dmean[,n+1]
plot <- plot(x = dmean$time, y, type = "b",
             cex = 0.8, lwd = 1.2,pch = 16, lty = 2,col = mycolor[5],
             main = paste("Diet",n),
             xlab = "", ylab = "",
             cex.lab = 1.2, cex.main = 1.5, cex.sub = 1.2,
             font.lab = 1, font.main = 4,
             family = "serif",
             col.lab = mycolor[5],col.main ="blue",
             frame.plot = FALSE,
             xlim = c(0,22), ylim = c(0, 280),
             axes = FALSE)
xx <- axis(1, at = seq(0,22,2), labels = as.character(seq(0,22,2)), 
           col = "blue",col.ticks = "blue",col.axis = "blue",
           las = 1,font.axis = 3)
yy <- axis(2, at = seq(0, 280, 50), labels = as.character(seq(0, 280, 50)),
           col = "blue", col.ticks = "blue", col.axis = "blue",
           las = 1,font.axis = 3)

mit <- minor.tick(nx = 5, ny = 5, tick.ratio = 0.5, 
                  x.args = list(col = "blue"),
                  y.args = list(col = "blue"))
abl <- abline(v = seq(5,25,5), h = seq(50,280,50), lty = 3, col = "orange")
txt <- text(dmean$time, y,as.character(round(y,0)), cex = 0.6, pos = 3, col = "red")
par(opar)
title(sub = "食用不同饲料小鸡平均体重变化趋势" ,font.sub = 2)
Rplot2.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,839评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,543评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,116评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,371评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,384评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,111评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,416评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,053评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,558评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,007评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,117评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,756评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,324评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,315评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,539评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,578评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,877评论 2 345

推荐阅读更多精彩内容