R語言實戰學習筆記-基本圖形的繪制


 

本文主要介紹R語言中基本圖形的繪制,包含以下幾種圖形:1.條形圖 2.餅圖 3.直方圖 4.核密度圖 5.箱線圖 6.點圖

1.直方圖的繪制

 

#直方圖繪制
barplot(height) #height是一個向量或者矩陣
a<-c(1,2,3,4,5,6) #垂直直方圖
barplot(a,main="Simple Bar Plot",xlab="Improtved",ylab="Frequency") #水平直方圖
barplot(a,main="Simple Bar Plot",xlab="Improtved",ylab="Frequency",horiz=TRUE)

 

 

 

#barplot(height) #height為矩陣時,繪制的是堆砌條形圖或分組條形圖
library(vcd) counts<-table(Arthritis$Improved,Arthritis$Treatment) #繪制堆疊圖
barplot(counts,main="Stacked Bar Plot",xlab="Treatment",ylab="Frquency",col=c("red","yellow","green"),legend=rownames(counts)) #繪制分組圖
barplot(counts,main="Grouped Bar Plot",xlab="Treatment",ylab="Grequency",col=c("red","yellow","green"),legend=rownames(counts),beside=TRUE)
#下面為counts的值

Placebo Treated
None 29 13
Some 7 7
Marked 7 21

#直方圖的微調 #增加Y邊界的大小
par(mar=c(5,8,4,2)) #旋轉條形的標簽
par(las=2) counts<-table(Arthritis$Improved) barplot(counts,main="Treatment Outcome",horiz=TRUE,cex.names=0.8,names.arg=c("No improved","some Improved","Marked Improved"))

#簡單的棘狀圖
library(vcd) attach(Arthritis) counts<-table(Treatment,Improved) spine(counts,main="Spinogram Example") detach(Arthritis)

2.餅圖的繪制

#餅圖的繪制
pie(x,labels) #x為一個非負值向量,表示每個扇形的面積,labels為各個扇形標簽的字符型向量
par(mfrow=c(2,2)) slices<-c(10,12,4,16,8) #基本的餅圖
lbls<-c("UK","US","Australia","Germany","France") pie(slices,labels=lbls,main="Simple Pie Chart") #為餅形圖添加比例數值
pct<-round(slices/sum(slices)*100) lbls2<-paste(lbls,"",pct,"%",sep="") pie(slices,labels=lbls2,col=rainbow(length(lbls2)),main="pie chart with Percentages") #3D餅形圖
library(plotrix) pie3D(slices,labels=lbls,explode=0.1,main="3D Pie Chart") #從表格創建餅圖
mytable<-table(state.region) lbls3<-paste(names(mytable),"\n",mytable,sep="") pie(mytable,labels=lbls3,main="Pie Chart form a Table")

#繪制容易看出大小的餅圖
library(plotrix) slices<-c(10,12,4,16,8) lbls<-c("US","UK","Australia","Germany","France") fan.plot(slices,labels=lbls,main="Fan Plot")

 

 

 3.繪制直方圖

直方圖:通過在X軸上將值域分割為一定數量的組,在Y軸上顯示相應的頻數,展示連續變量的分析

#繪制直方圖
par(mfrow=c(2,2)) #簡單的直方圖
hist(mtcars$mpg) #設置直方圖的顏色,添加坐標軸標簽
hist(mtcars$mpg,breaks=12,col="red",xlab="Miles Per Gallon", main="Colored histogram with 12 bins") #添加軸須圖
hist(mtcars$mpg,freq=FALSE,breaks=12,col="red",xlab="Miles per Gallon",main="Histogram ,rug plot,density curve") rug(jitter(mtcars$mpg)) lines(density(mtcars$mpg),col="blue",lwd=2) #添加正態分布密度曲線和外框
x<-mtcars$mpg h<-hist(x,breaks=12,col="red",xlab="Miles Per Gallon",main="Histogram with normal curve and box") xfit<-seq(min(x),max(x),length=40) yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) yfit<-yfit*diff(h$mids[1:2]*length(x)) lines(xfit,yfit,col="blue",lwd=2) box()
#上述圖形比較復雜,后續結合具體實例深入研究

4.繪制核密度圖

核密度:核密度估計師用於估計隨機變量概率密度函數的一種非參數方法

#繪制核密度圖
par(mfrow=c(2,1)) d<-density(mtcars$mpg) plot(d) d<-density(mtcars$mpg) plot(d,main="kernel Density of Miles Per Gallon") polygon(d,col="red",border="blue") rug(mtcars$mpg,col="brown")

 

 

#一個圖形中繪制多個核密度圖
library(sm) attach(mtcars) cyl.f<-factor(cyl,levels=c(4,6,8),labels=c("4 cylinder","6 cylinder","8 cylinder")) sm.density.compare(mpg,cyl,xlab="Miles Per Gallon") title(main="MPG Distribution by Car Cylinders") detach(mtcars)

5.箱形圖

箱形圖:通過繪制連續性變量的五數總括,即最小值、下四分位、中位數、上四分位數、最大值

boxplot(mtcars$mpg,main="Box plot",ylab="Miles per Gallon")

#一個圖中繪制多個箱形圖
boxplot(formula,data=data.frame) boxplot(mpg~cyl,data=mtcars,main="Car Mileage Data",xlab="Number of Cylinders",ylab="Miles Per Gallon")

#凹槽型箱形圖
boxplot(mpg~cyl,data=mtcars,notch=TRUE,col="red",main="Car Mileage Data",xlab="Number of Cylinders",ylab="Miles Per Gallon")

 

#繪制交叉因子箱形圖
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")) boxplot(mpg~am.f *cyl.f, data=mtcars, varwidth=TRUE, col=c("gold","darkgreen"), main="MPG Distribution by Auto Type", xlab="Auto Type", ylab="Miles Per Gallon")

#小提琴箱形圖
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"),col="gold")

6.點圖

#水平刻度上繪制大量有標簽值的方法
dotchart(x,labels) dotchart(mtcars$mpg,labels=row.names(mtcars),cex=0.7,main="Gas Mileafe for car Models",xlab="Miles Per Gallon")

#進行調整的點圖
x<-mtcars[order(mtcars$mpg),] x$cyl<-factor(x$cyl) 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=0.7,groups=x$cyl,gcolor="black",color=x$color,pch=19,main="Gas Mileage for car Models",xlab="Miles Per Gallon")

以上簡單探索了R語言中的集中常用圖形,后續要根據具體業務需要靈活運用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM