數(shù)據(jù)可視化,絕對(duì)是一門藝術(shù)。完美的數(shù)據(jù),需要有完美的圖片來展示。人類非常善于從視覺呈現(xiàn)中洞察關(guān)系。一幅精心繪制的圖形能夠幫助你在數(shù)以千計(jì)的零散信息中做出有意義的比較,提煉出使用其他方法時(shí)不那么容易發(fā)現(xiàn)的模式。而R具有頂尖水準(zhǔn)的制圖功能,進(jìn)行很好的可視化。



3.1 使用圖形
R是一個(gè)驚艷的圖形構(gòu)建平臺(tái)。在通常的交互式會(huì)話中,你可以通過逐條輸入語句構(gòu)建圖形,逐漸完善圖形特征,直至得到想要的結(jié)果。
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
detach(mtcars)
3.2 一個(gè)簡單的例子

# 數(shù)據(jù)導(dǎo)入R中
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
# 繪圖,對(duì)藥物A的響應(yīng)
plot(dose, drugA, type="b")

plot()是一個(gè)函數(shù),plot(x, y, type="b"),將x作為橫軸,y置于縱軸,繪制點(diǎn)集,連線。選項(xiàng)type=“b”表示同時(shí)繪制點(diǎn)和線,具體的可以用
?plot查看幫助選項(xiàng)。
3.3.1 符號(hào)和線條
使用圖形參數(shù)來指定繪圖時(shí)使用的符號(hào)和線條類型。


3.3.2 顏色
在R語言中,可以通過下標(biāo)、顏色名稱、十六進(jìn)制的顏色值、RGB值或HSV值來指定顏色。函數(shù)colors()可以返回所有可用顏色的名稱。
# RColorBrewer 這個(gè)包是用來創(chuàng)建吸引人的顏色配對(duì)
install.packages("RColorBrewer")
library(RColorBrewer)
n <- 7
# 產(chǎn)生七種顏色的向量,
mycolors <- brewer.pal(n, "Set1")
# 最后畫出BAR圖
barplot(rep(1,n), col=mycolors)


gray()函數(shù)可以生成多階灰度色,rainbow()可以生成連續(xù)的“彩虹型”顏色
n <- 10
mycolors <- rainbow(n)
pie(rep(1, n), labels=mycolors, col=mycolors)
mygrays <- gray(0:n/n)
pie(rep(1, n), labels=mygrays, col=mygrays)


3.3.3 文本屬性
圖形參數(shù)同樣可以用來指定字號(hào)、字體和字樣。



3.3.4 圖形尺寸與邊界尺寸

- 通過圖形參數(shù)控制圖形外觀
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
# 生成可以修改的當(dāng)前圖形參數(shù)列表
opar <- par(no.readonly=TRUE)
# 2英寸寬,3英寸高
par(pin=c(2, 3))
# 線條寬度為默認(rèn)的2倍,符號(hào)為默認(rèn)的1.5倍
par(lwd=2, cex=1.5)
# 坐標(biāo)軸刻度文本被設(shè)置為斜體,縮小為原來的75%
par(cex.axis=.75, font.axis=3)
# 使用紅色的實(shí)心圓圈和虛線創(chuàng)建第一幅圖
plot(dose, drugA, type="b", pch=19, lty=2, col="red")
# 使用綠色填充的綠色菱形加藍(lán)色虛線創(chuàng)建了第二幅圖
plot(dose, drugB, type="b", pch=23, lty=6, col="blue", bg="green")
# 恢復(fù)初始的圖形參數(shù)設(shè)置
par(opar)


par()的參數(shù)設(shè)定對(duì)兩幅圖都有效,而在plot()函數(shù)中指定的函數(shù)只對(duì)當(dāng)前的圖有效。
3.4 添加文本、自定義坐標(biāo)軸和圖例
可以添加標(biāo)題main、副標(biāo)題sub、坐標(biāo)軸標(biāo)簽(xlab、ylab)并指定了坐標(biāo)軸范圍(xlim、ylim)。當(dāng)然并不是所有的函數(shù)都支持這些選項(xiàng)。
plot(dose, drugA, type="b",
col="red", lty=2, pch=2, lwd=2,
main="Clinical Trials for Drug A",
sub="This is hypothetical data",
xlab="Dosage", ylab="Drug Response",
xlim=c(0, 60), ylim=c(0, 70))

3.4.1 標(biāo)題
title()函數(shù)可以為圖形添加標(biāo)題和坐標(biāo)軸標(biāo)簽,當(dāng)然也是可以指定其他圖形參數(shù)。調(diào)用形式:
title(main="", sub="", xlab="", ylab="", col.main="")
3.4.2 坐標(biāo)軸
可以使用axis()函數(shù)來創(chuàng)建自定義的坐標(biāo)軸,但是你要禁止使用繪圖函數(shù)自動(dòng)生成的坐標(biāo)軸,axes=FALSE禁用全部坐標(biāo)軸,坐標(biāo)軸創(chuàng)建的選項(xiàng):

自定義坐標(biāo)軸的例子:
x <- c(1:10)
y <- x
z <- 10/x
# 保存當(dāng)前的所有參數(shù)
opar <- par(no.readonly=TRUE)
# 設(shè)置邊界大小
par(mar=c(5, 4, 4, 8) + 0.1)
# 繪圖,符號(hào)是21,線是紅色,
plot(x, y, type="b",
pch=21, col="red",
yaxt="n", lty=3, ann=FALSE)
# 在圖上繪制第二條線,藍(lán)色
lines(x, z, type="b", pch=22, col="blue", lty=2)
# 在左邊建立y軸,坐標(biāo)軸刻度為x向量,顏色紅色,垂直于坐標(biāo)抽
axis(2, at=x, labels=x, col.axis="red", las=2)
# 在右邊建立坐標(biāo)軸,刻度為z向量保留兩位小數(shù),藍(lán)色,垂直,刻度文字為70%,刻度線向外0.01
axis(4, at=z, labels=round(z, digits=2),
col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
# 為坐標(biāo)軸添加文本,右邊,添加y=1/x,垂直于坐標(biāo),藍(lán)色,正常大小
mtext("y=1/x", side=4, line=3, cex.lab=1, las=2, col="blue")
# 添加坐標(biāo)軸標(biāo)簽
title("An Example of Creative Axes",
xlab="X values",
ylab="Y=X")
par(opar)

3.4.3 參考線
abline()可以用來添加參考線,格式:abline(h=yvalues, v=xvalues),里面也是可以指定其他圖形參數(shù)。
3.4.4 圖例
當(dāng)圖形中包含的數(shù)據(jù)不止一組時(shí),圖例可以判斷。legend()來添加圖例:legend(location, title, legend, ...)
兩種藥物響應(yīng)情況:
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly=TRUE)
par(lwd=2, cex=1.5, font.lab=2)
plot(dose, drugA, type="b",
pch=15, lty=1, col="red", ylim=c(0, 60),
main="Drug A vs. Drug B",
xlab="Drug Dosage", ylab="Drug Response")
lines(dose, drugB, type="b",
pch=17, lty=2, col="blue")
abline(h=c(30), lwd=1.5, lty=2, col="gray")
library(Hmisc)
minor.tick(nx=3, ny=3, tick.ratio=0.5)
legend("topleft", inset=.05, title="Drug Type", c("A","B")
lty=c(1, 2), pch=c(15, 17), col=c("red", "blue"))
par(opar)

3.4.5 文本標(biāo)注
通過函數(shù)text()和mtext()將文本添加到圖形上。前者可向繪圖區(qū)域內(nèi)部添加文本,而后者向圖形的四個(gè)邊界之一添加文本。

示例:
attach(mtcars)
plot(wt, mpg,
main="Mileage vs. Car Weight",
xlab="Weight", ylab="Mileage",
pch=18, col="blue")
text(wt, mpg,
row.names(mtcars),
cex=0.6, pos=4, col="red")
detach(mtcars)

3.4.6 數(shù)學(xué)標(biāo)注
函數(shù)plotmath()可以為圖形主體或邊界上的標(biāo)題、坐標(biāo)軸名稱或文本標(biāo)注添加數(shù)學(xué)符號(hào)。
3.5 圖形組合
在R中使用函數(shù)par()和layout()可以組合多幅圖形為一幅總括圖形。圖形參數(shù)mfrow=c(nrows, ncols)按行填充和mfcol=c(nrows, ncol)按列填充。示例:
attach(mtcars)
opar <- par(no.readonly=TRUE)
# 按行填充,2x2矩陣
par(mfrow=c(2,2))
# 畫四幅圖
plot(wt,mpg, main="Scatterplot of wt vs. mpg")
plot(wt,disp, main="Scatterplot of wt vs. disp")
hist(wt, main="Histogram of wt")
boxplot(wt, main="Boxplot of wt")
par(opar)
detach(mtcars)

PS:這一部分內(nèi)容確實(shí)比較多,整個(gè)下來比較累,進(jìn)度比較慢,圖形的參數(shù)確實(shí)比較多,要多練習(xí),多用才可以。