R基礎繪圖包之plot及參數(shù)

這篇文章可以讓你學到什么宵荒?

可以讓你把這樣的圖:

Rplot01.png

通過設置各種參數(shù)修改為這樣自定義的圖:

Rplot2.png

一、 本文使用的數(shù)據(jù)

我們使用R內置的數(shù)據(jù)集Chickweight來進行畫圖的學習,首先了解一下數(shù)據(jù)的大致情況

  1. 查看幫助文檔

因為Chickweight是R內置數(shù)據(jù)集报咳,可以查看幫助文檔來了解數(shù)據(jù)

? ChickWeight

從幫助文檔中可以看到這個數(shù)據(jù)集是一個578行侠讯,4列的數(shù)據(jù)框,是剛出生的小雞吃四種不同食物體重變化情況的數(shù)據(jù)暑刃,每個變量的含義分別是:

weight: 體重厢漩,數(shù)值型數(shù)據(jù)
Time: 時間,小雞的日齡岩臣,數(shù)值型數(shù)據(jù)
Chick: 小雞的編號溜嗜,因子型數(shù)據(jù)
Diet: 食物的編號,因子型數(shù)據(jù)

  1. 使用基礎函數(shù)了解數(shù)據(jù)
# 數(shù)據(jù)賦值給mydata
mydata <- ChickWeight
# 查看前幾行
head(mydata)
# 查看數(shù)據(jù)的類型
class(mydata)
# 了解數(shù)據(jù)的底細
str(mydata)
# 了解數(shù)據(jù)的概況
summary(mydata)

二架谎、 畫圖函數(shù)及參數(shù)的學習

Step1 使用默認參數(shù)畫出圖形

1.1 畫出1號小雞體重隨著時間變化的情況

# 判斷第三列Chick等于1炸宵,然后用中括號提取出僅僅包含1號小雞的數(shù)據(jù),命名為chick1
chick1 <- mydata[mydata$Chick == 1, ]

# 以chick1的日齡為橫坐標谷扣,體重為縱坐標土全,不設置任何參數(shù),默認畫出散點圖
plot(x = chick1$Time, y = chick1$weight)

Step2 修改圖形主體部分

2.1 修改圖形的類型

我不喜歡這個只有點的圖会涎,想要修改一下裹匙,怎么做呢?

  • 參數(shù)

增加一個type參數(shù)在塔,即可修改圖形的類型了

# 設置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 修改圖形元素大小

我選擇了既有點有又有線的圖绰沥,現(xiàn)在我想要把點調大點,線調粗一點贺待,應該怎么做呢徽曲?

  • 參數(shù)

cex : 修改點圖中點的大小
lwd : 修改線圖中線的粗細

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

cex : 默認大小的任意倍數(shù)
lwd : 默認粗細的任意倍數(shù)

6.png

2.3 修改圖形元素的形狀

我想把空心圓和實線修改成實心圓點和虛線要怎么做麸塞?

  • 參數(shù)

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 修改圖形元素的顏色

  • 參數(shù)

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 修改圖形標題部分

我們會發(fā)現(xiàn)plot()的函數(shù)給x軸和y軸標題默認設置為變量的名稱稚虎,但是我們不喜歡,想要修改偎捎,并且想給整個圖形加上大標題蠢终,應該怎么做呢序攘?

3.1 設置圖形標題

  • 參數(shù)

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 修改文字大小

  • 參數(shù)

cex.axis : 坐標軸刻度文字大小
cex.lab: 坐標軸標題文字大小
cex.main : 主標題文字大小
cex.sub :次標題文字大小
以上參數(shù)均與cex參數(shù)類似,設置默認大小的倍數(shù)即可相應放大或縮小

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 修改文字字體
  • 參數(shù)

font.axis : 設置坐標刻度字體寻拂,1常規(guī)字體程奠,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 修改標題文字顏色

  • 參數(shù)

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 添加標題和禁用標題

  • 參數(shù)

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)
  • 函數(shù)

title() : 使用title()函數(shù)可以為圖形另外設置標題,其參數(shù)與plot()函數(shù)中設置標題的參數(shù)相同

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()函數(shù)就可以將x和y軸的標題設置為不同的顏色了晶姊!
title(ylab = "Weight",
      col.lab = "orange",
      cex.lab = 1.5, font.lab = 3)
3.5.png

Step4 修改圖形坐標軸與邊框

4.1 隱藏框架線及坐標軸

  • 參數(shù)

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 設置坐標軸取值范圍

  • 參數(shù)

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 自定義坐標軸

  • 函數(shù)

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

  • 參數(shù)

1: 設置x軸
2: 設置y軸
at : 設置刻度所在位置的们衙,數(shù)值向量
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")

  • 函數(shù)

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

  • 參數(shù)

nx : x軸加入次要刻度線之后把原來的每個分成多少格
ny : y軸加入次要刻度線之后把原來的每個分成多少格
tick.ratio : 次要刻度線相對主要刻度線的大小
x.args : 用來設置x軸的其他參數(shù)的列表忆蚀,參數(shù)同axis(), 比如col等
y.args : 用來設置y軸的其他參數(shù)的列表矾利,參數(shù)同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 修改圖形整體尺寸大小

  • 函數(shù)

par()

  • 參數(shù)

no.readonly = TRUE : 設置畫板的參數(shù)為可修改狀態(tài)
pin: 以英寸表示的圖形尺寸(寬和高)
mai: 以數(shù)值向量表示的邊界大小,順序為“下馋袜、左男旗、上、右”欣鳖,單位為英寸
mar: 以數(shù)值向量表示的邊界大小察皇,順序為“下、左泽台、上什荣、右”,單位為英分怀酷。默認(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 添加參考線

  • 函數(shù)

abline()

  • 參數(shù)

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 添加圖例

  • 函數(shù)

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

  • 參數(shù)

"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 添加文本標注

  • 函數(shù)

text()向繪圖區(qū)域內部添加文本
text(x, y , labels , pos , cex , col )

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

  • 參數(shù)

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號小雞的體重變化圖,現(xiàn)在我們想要畫出更多小雞的體重變化圖形笛求,并且把它們組合到一起對比廊移,應該怎么辦呢?

7.1 使用pai()中的mfrowmfcol參數(shù)組合圖形

  • 先分別提取出各個小雞的數(shù)據(jù)來:
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 使用函數(shù)layout()組合圖形

  • 參數(shù)

mat : 一個矩陣探入,設置圖形的總體布局
widths: 向量狡孔,設置圖形矩陣中每列相對寬度
heights: 向量,設置圖形矩陣中每行的相對高度

運行以下代碼蜂嗽,即可理解layout()的布局規(guī)律:

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參數(shù)設置圖形位置,疊加圖形

  • 參數(shù)

fig
整個畫布大可以理解為fig = c(0,1,0,1), 向量中的4個數(shù)依次代表: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

三界阁、 實戰(zhàn)演練

需求

畫出食用不同飼料的小雞,平均體重隨著時間變化的趨勢圖胖喳,并且組合到一張圖片中對比

數(shù)據(jù)處理

# 提取數(shù)據(jù)
mydata <- ChickWeight
# 把變量名稱全部改成小寫
names(mydata) <- tolower(names(mydata))

# 將原始的寬型數(shù)據(jù)轉換為長型數(shù)據(jù)
library(reshape2)
mydatam <- melt(mydata, id = 2:4, na.rm = TRUE)

# 使用dcast函數(shù)泡躯,將數(shù)據(jù)整理成吃不同食物小雞每天的平均體重
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
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市丽焊,隨后出現(xiàn)的幾起案子较剃,更是在濱河造成了極大的恐慌,老刑警劉巖技健,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件写穴,死亡現(xiàn)場離奇詭異,居然都是意外死亡雌贱,警方通過查閱死者的電腦和手機啊送,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進店門偿短,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人馋没,你說我怎么就攤上這事昔逗。” “怎么了篷朵?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵勾怒,是天一觀的道長。 經(jīng)常有香客問我声旺,道長笔链,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任腮猖,我火速辦了婚禮鉴扫,結果婚禮上,老公的妹妹穿的比我還像新娘澈缺。我一直安慰自己幔妨,他們只是感情好,可當我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布谍椅。 她就那樣靜靜地躺著,像睡著了一般古话。 火紅的嫁衣襯著肌膚如雪雏吭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天陪踩,我揣著相機與錄音杖们,去河邊找鬼。 笑死肩狂,一個胖子當著我的面吹牛摘完,可吹牛的內容都是我干的。 我是一名探鬼主播傻谁,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼孝治,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了审磁?” 一聲冷哼從身側響起谈飒,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎态蒂,沒想到半個月后杭措,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡钾恢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年手素,在試婚紗的時候發(fā)現(xiàn)自己被綠了鸳址。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡泉懦,死狀恐怖稿黍,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情祠斧,我是刑警寧澤闻察,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布,位于F島的核電站琢锋,受9級特大地震影響辕漂,放射性物質發(fā)生泄漏。R本人自食惡果不足惜吴超,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一钉嘹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鲸阻,春花似錦跋涣、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至细诸,卻和暖如春沛贪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背震贵。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工利赋, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人猩系。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓媚送,卻偏偏與公主長得像,于是被迫代替她去往敵國和親寇甸。 傳聞我的和親對象是個殘疾皇子塘偎,可洞房花燭夜當晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內容