這篇文章可以讓你學到什么宵荒?
可以讓你把這樣的圖:
通過設置各種參數(shù)修改為這樣自定義的圖:
一、 本文使用的數(shù)據(jù)
我們使用R內置的數(shù)據(jù)集Chickweight
來進行畫圖的學習,首先了解一下數(shù)據(jù)的大致情況
- 查看幫助文檔
因為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ù)
- 使用基礎函數(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"
:不畫圖蛔溃,只畫出坐標
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ù)
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取值類型:
lty取值類型:
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)
我比較喜歡上面的第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)
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"))
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)
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)
Step7 組合多個圖形
上面我們畫出了1號小雞的體重變化圖,現(xiàn)在我們想要畫出更多小雞的體重變化圖形笛求,并且把它們組合到一起對比廊移,應該怎么辦呢?
7.1 使用pai()
中的mfrow
與mfcol
參數(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)
三界阁、 實戰(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)