記錄下條形圖雌澄,直方圖陡鹃,箱線圖以及其它部分常見統(tǒng)計圖的函數(shù)與技巧~
1绰疤、條形圖
barplot()函數(shù):針對類別型變量澡谭,頻率分類計數(shù)
tip:原始數(shù)據(jù)先table
硼被,再繪圖
library(vcd) #示例用vcd包的一項數(shù)據(jù)
Arthritis$Improved
#觀察變量類型為類別型
table(Arthritis$Improved)
counts <- table(Arthritis$Improved)
# 針對分類變量統(tǒng)計計數(shù)
counts
class(counts)
#為專有的二聯(lián)表格式
str(counts)
barplot(counts,
main="Simple Bar Plot", col = rainbow(3),
xlab="Improvement", ylab="Frequency",)
#添加horiz=TRUE 參數(shù)會生成水平條形圖
barplot
- 堆砌條形圖與分組條形圖
#若對象是一個矩陣
counts <- table(Arthritis$Improved, Arthritis$Treatment)
#counts有兩組(對照組蝗碎,改善組)喇澡,每組針對效果又分為三種類型
#堆砌條形圖腕侄,besides=FALSE(默認值)
barplot(counts,
main="Stacked Bar Plot",
xlab="Treatment", ylab="Frequency",
col=c("red", "yellow","green"),
legend=rownames(counts))
堆砌條形圖.png
#分組條形圖钦铺,需要設置參數(shù)besides=TRUE订雾。二者區(qū)別可見圖
barplot(counts,
main="Grouped Bar Plot",
xlab="Treatment", ylab="Frequency",
col=c("red", "yellow", "green"),
legend=rownames(counts), beside=TRUE)
分組條形圖.png
補充一個棘狀圖:
spine
是每個條形高度均為1,每一段的高度表示比例的堆砌條形圖
attach(Arthritis)
counts <- table(Treatment,Improved)
spine(counts, main="Spinogram Example")
detach(Arthritis)
棘狀圖.png
關于條形圖并非一定是根據(jù)頻率矛洞,也可以利用整合函數(shù)(aggregate)創(chuàng)建均值洼哎、中位數(shù)等條形圖烫映。
下面以繪制均值條形圖為例,順便復習下整合函數(shù)aggregate的用法
states <- data.frame(state.region, state.x77)
means <- aggregate(states$Illiteracy, by=list(state.region), FUN=mean)
#參數(shù)依次為-所想要返回的變量-整合依據(jù)-整合功能
means #查看下返回的結果
barplot(means$x, names.arg=means$Group.1,col = rainbow(4)) #name.arg選項為展示標簽
title("Mean Illiteracy Rate") #添加個大標題
image.png
2噩峦、直方圖:hist()
- 用于展示連續(xù)性變量頻數(shù)分布圖锭沟。x軸是將值域分割為一定數(shù)量的組,y軸為頻數(shù)识补。
hist(x) #x是數(shù)值型向量
mtcars$mpg #查看下族淮,確實為一連串數(shù)值
hist(mtcars$mpg)
#最簡單的畫法,參數(shù)均為默認
hist
變化分組凭涂,上色等
hist(mtcars$mpg,
breaks=12,
col=rainbow(12),
xlab="Miles Per Gallon",
main="Colored histogram with 12 bins")
#上述參數(shù)依次為:指定分為12組祝辣;上色;添加標題切油。
- 修飾1:添加一條軸須圖蝙斜,是一種一維的表達方式∨旌看x軸~
rug(jitter(mtcars$mpg, amount = 0.01))
- 修飾2:添加正態(tài)分布曲線
xfit <- seq(min(mtcars$mpg), max(mtcars$mpg), length = 40)
yfit <- dnorm(xfit, mean = mean(mtcars$mpg), sd = sd(mtcars$mpg))
yfit <- yfit*diff(h$mids[1:2])*length(x)
# 確定正態(tài)分布點
lines(xfit, yfit, col = "blue", lwd = 2)
直方圖
3孕荠、箱線圖
描述了連續(xù)型變量的分布
五個數(shù):最小值、下四分位數(shù)(.25)滤馍、中位數(shù)(.5)岛琼、上四分位數(shù)(.75),最大值巢株。
能夠判斷離群點(IQR±1.5*IQR以外的值槐瑞,IQR=四分位距,即上四分位數(shù)與下四分位數(shù)的差值阁苞。若數(shù)據(jù)點未達到計算范圍兩端困檩,則由這些數(shù)據(jù)點的高值和低值(不包括離群值)來確定須線。
#(1)直接繪圖單箱線圖
boxplot(mtcars$mpg, ylim=c(5,35))
#ylim參數(shù)指定y軸的起始位置
boxplot1
#(2)單因子并列箱線圖
mtcars$cyl.f <- factor(mtcars$cyl,
levels=c(4,6,8),
labels=c("cyl4","cyl6","cyl8"))
boxplot(mpg~cyl.f,data=mtcars,
main="Car Milage Data",
xlab="Number of Cylinders",
ylab="Miles Per Gallon")
#針對cyl三種不同的類型那槽,畫三個箱線圖
單因子.png
#(3)交叉雙因子并列箱線圖
mtcars$cyl.f <- factor(mtcars$cyl,
levels=c(4,6,8),
labels=c("4","6","8"))
mtcars$am.f <- factor(mtcars$am,
levels=c(0,1),
labels=c("auto","standard"))
#上述將兩個數(shù)值型變量轉換為因子型
boxplot(mpg ~ am.f *cyl.f,
data=mtcars,
varwidth=TRUE,
col=c("gold", "darkgreen"),
main="MPG Distribution by Auto Type",
xlab="Auto Type")
#varwidth=TRUE 表示箱圖的寬度可適當變化悼沿。
交叉因子.png
補充:畫含有凹槽的箱線圖,boxplot() 函數(shù)添加notch=TRUE參數(shù)骚灸,若兩個箱的凹槽不重疊糟趾,則表明其中位數(shù)有顯著差異。
4甚牲、點圖
-
dotchart(x,label=)
函數(shù)
dotchart(mtcars$mpg,labels=row.names(mtcars),
cex=.7,
main="Gas Mileage for Car Models",
xlab="Miles Per Gallon")
#上面是個簡單的點圖义郑;下面來繪制個經(jīng)排序、分組丈钙,著色后精美的點圖
x <- mtcars[order(mtcars$mpg),]
x$cyl <- factor(x$cyl)
#轉變?yōu)橐蜃有头峭裕瑸榉纸M做準備
x$color[x$cyl==4] <- "red"
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen"
#上三行代碼為每個類別記錄為一種顏色,單設一列雏赦,為后面著色做準備
dotchart(x$mpg,
labels = row.names(x),
cex=.7,
pch=19,
groups = x$cyl,
gcolor = "black",
color = x$color,
main = "Gas Mileage for Car Models\ngrouped by cylinder",
xlab = "Miles Per Gallon")
點圖.png
5劫笙、其它圖形
核密度圖
- 估計連續(xù)型變量概率分布的非參數(shù)方法
如前芙扎,正態(tài)分布概率密度曲線是有參數(shù)的(均值mean,方差sd)填大;而核密度圖僅根據(jù)數(shù)據(jù)特征估計概率密度的方法戒洼。
plot(density(x))
d=density(mtcars$mpg)
plot(d)
polygon(d,col="red",border="blue") #在下方添加實心顏色
核密度.png
- 繪制多條核密度曲線,比較組間差別
library(sm) #需要加載一個包
cyl.f <- factor(mtcars$cyl, levels = c(4,6,8),
labels = c("4 cylinder", "6 cylinder",
"8 cylinder"))
sm.density.compare(mtcars$mpg, mtcars$cyl, xlab = "Miles Per Gallon")
colfill <- c(2:(1 + length(levels(cyl.f)))) #注意顏色對應的數(shù)字
legend(locator(1), levels(cyl.f), fill = colfill)
#交互式確定圖例位置
多條核密度曲線比較
小提琴圖
- 就是核密度圖以鏡像的方式在箱線圖上的疊加允华,白點為中位數(shù)施逾。
- 值得注意的是按因子繪制多類小提琴圖時,
vioplot
函數(shù)要求要把不同組分(類別)分離到不同的變量中例获。
library(vioplot)
x1 <- mtcars$mpg[mtcars$cyl==4]
x2 <- mtcars$mpg[mtcars$cyl==6]
x3 <- mtcars$mpg[mtcars$cyl==8]
vioplot(x1, x2, x3,
names=c("4 cyl", "6 cyl", "8 cyl"))
小提琴圖.png
還有餅圖的做法,覺得用到的機會不多曹仗,就不介紹了榨汤,詳見p116
代碼大部分參考教材《R語言實戰(zhàn)(第2版)》
--寒假自學R語言的生信小白。
武漢加油怎茫!