R語言繪圖-(2)_第1頁
R語言繪圖-(2)_第2頁
R語言繪圖-(2)_第3頁
R語言繪圖-(2)_第4頁
R語言繪圖-(2)_第5頁
已閱讀5頁,還剩43頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、2培訓培訓2 R繪圖繪圖統(tǒng)計繪圖統(tǒng)計繪圖 統(tǒng)計圖形易于觀察到數(shù)據(jù)中的信息。統(tǒng)計圖形易于觀察到數(shù)據(jù)中的信息。 統(tǒng)計學家統(tǒng)計學家John Tukey: “圖形的最大價值就圖形的最大價值就是使我們注意到從來沒有料到過的信息。是使我們注意到從來沒有料到過的信息。” “一圖勝千言一圖勝千言”:尤其數(shù)據(jù)龐大時:尤其數(shù)據(jù)龐大時.2 R 繪圖繪圖 Bar plot條形圖條形圖 Pie plot餅圖餅圖 Histogram直方圖直方圖 Density plot-核密度圖核密度圖 Dot plot散點圖散點圖 Heatmap熱圖熱圖 Contour Plot-等高圖等高圖 Perspective Plot-三維圖

2、三維圖 Violin Plot-小提琴圖小提琴圖 Scatter plot Matrices散點矩陣圖散點矩陣圖 Conditioning plot條件圖條件圖 Correlograms相關圖相關圖 Map地圖地圖High level-高級繪圖高級繪圖 lattice包包 ggplot2包包2.1 條形圖條形圖(Bar plots)條形圖是目前各種統(tǒng)計圖形中應用最廣泛的條形圖是目前各種統(tǒng)計圖形中應用最廣泛的圖形之一,通過垂直或水平的條形展示類別圖形之一,通過垂直或水平的條形展示類別變量的頻數(shù)分布。變量的頻數(shù)分布。條形圖繪制函數(shù)為條形圖繪制函數(shù)為 barplot(),其用法如下:,其用法如下:

3、barplot(x) 其中,參數(shù)其中,參數(shù) x 是一個向量或矩陣。是一個向量或矩陣。條形圖例子條形圖例子par(mfrow = c(1, 2) barplot(counts, main = Simple Bar Plot, xlab= Improvement, ylab= Frequency) barplot(counts, main = Horizontal Bar Plot, xlab= Frequency, ylab= Improvement, horiz = TRUE) library(vcd) counts counts None Some Marked 42 14 28NoneMa

4、rkedSimple Bar PlotImprovementFrequency010203040NoneMarkedHorizontal Bar PlotFrequencyImprovement010203040堆砌和分組條形圖堆砌和分組條形圖counts - table(Arthritis$Improved,Arthritis$Treatment)barplot(counts, # 繪制堆砌繪制堆砌條形圖條形圖 main = Stacked Bar Plot, xlab = Treatment, ylab=Frequency, col = c(red, yellow,green), lege

5、nd = rownames(counts) barplot(counts, # 繪制分組繪制分組條形圖條形圖 main = Grouped Bar Plot, xlab = Treatment, ylab = Frequency, col = c(red, yellow, green), legend = rownames(counts), beside = TRUE)PlaceboTreatedStacked Bar PlotTreatmentFrequency010203040NoneSomeMarkedPlaceboTreatedGrouped Bar PlotTreatmentFreq

6、uency0102030NoneSomeMarked2.2 餅圖餅圖(Pie chart)餅圖:用于表示計數(shù)資料、質(zhì)量性狀資料的百分比。pie(x, labels) 其中,x :數(shù)值型向量,是扇形的面積;labels :字符型向量,是扇形的標簽。餅圖例子餅圖例子par(mfrow = c(1, 2) slices - c(12, 3, 26, 16, 11, 4) lbls - c(Blueberry, Cherry, Apple,Peach, Banana, other)pct - round(slices / sum(slices) * 100) lbls2 - paste(lbls, n

7、 , pct, %, sep = )pie(slices, labels = lbls2, col = rainbow(length(lbls2), main= Pie Chart with Percentages) Blueberry 17%Cherry 4%Apple 36%Peach 22%Banana 15%other 6%Pie Chart with Percentages3D餅圖餅圖3D Pie Chart BlueberryCherryApplePeachBananaother3D餅圖和扇形圖餅圖和扇形圖3D Pie Chart BlueberryCherryApplePeach

8、BananaotherFan PlotApplePeachBlueberryBananaotherCherry2.3 直方圖直方圖Historgram直方圖又稱為柱狀圖或質(zhì)量分布圖,由一系列高度不等的直方圖又稱為柱狀圖或質(zhì)量分布圖,由一系列高度不等的縱向條紋或線段表示數(shù)據(jù)分布的情況。縱向條紋或線段表示數(shù)據(jù)分布的情況。 一般用橫軸表示數(shù)據(jù)類型,縱軸表示分布情況。一般用橫軸表示數(shù)據(jù)類型,縱軸表示分布情況。直方圖是常用于展示連續(xù)數(shù)據(jù)分布,本質(zhì)上也是密度函數(shù)直方圖是常用于展示連續(xù)數(shù)據(jù)分布,本質(zhì)上也是密度函數(shù)的一種估計。的一種估計。 繪制直方圖方法如下:繪制直方圖方法如下: hist(x, breaks

9、 =, freq =) x: 數(shù)值向量,數(shù)值向量,breaks:組數(shù),組數(shù),freq:概率密度概率密度(F)或頻數(shù)或頻數(shù)(T) 。直方圖例子直方圖例子par(mfrow = c(2, 2), mar = c(2, 3, 2, 0.5), mgp = c(2, 0.5, 0)data(geyser, package = MASS)hist(geyser$waiting, main = (1) freq = TRUE, xlab = waiting)hist(geyser$waiting, freq = FALSE, xlab = waiting, main = (2) freq = FALSE)

10、hist(geyser$waiting, breaks = 5, density = 10, xlab = waiting, main = (3) breaks = 5)hist(geyser$waiting, breaks = 40, col = grey, xlab = waiting, main = (4) breaks = 40)(1) freq = TRUEwaitingFrequency4050607080901100204060(2) freq = FALSEwaitingDensity4050607080901100.000.020.04(3) breaks = 5waitin

11、gFrequency40506070809011004080(4) breaks = 40waitingFrequency405060708090 100010202.4 核密度圖核密度圖kernel density Plot核密度估計核密度估計(kernel density estimation)是概率論中用來是概率論中用來估計未知的密度函數(shù)的一種非參數(shù)檢驗方法。估計未知的密度函數(shù)的一種非參數(shù)檢驗方法。核密度圖是觀察連續(xù)型變量分布的有效方法之一。核密度圖是觀察連續(xù)型變量分布的有效方法之一。 核密度圖的繪圖方法為:核密度圖的繪圖方法為: plot(density(x) x: 是一個數(shù)值型向量。

12、是一個數(shù)值型向量。 核密度圖例子核密度圖例子par(mfrow = c(2,1)d - density(iris,1) plot(d) #第一張第一張 plot(d, main = Kernel Density of Sepal length) #第二張第二張 polygon(d, col = blue, border = red) 456780.00.20.4density.default(x = iris, 1)N = 150 Bandwidth = 0.2736Density456780.00.20.4Kernel Density of Sepal lengthN = 150 Bandw

13、idth = 0.2736Density2.5 散點圖散點圖(Scatter diagram)散點圖通常用來展示兩個變量之間的關系,這種關系可散點圖通常用來展示兩個變量之間的關系,這種關系可能是線性或非線性的。能是線性或非線性的。散點圖的繪圖方法如下:散點圖的繪圖方法如下: plot(x, y, type = ) x、y 為數(shù)值型向量,為數(shù)值型向量,type 為指定圖形的類型。為指定圖形的類型。par(mfrow = c(2, 2)tv- c(p, l, b, o)for(i in 1 : 4) plot(iris,1, iris,3, type = tvi, main = paste(typ

14、e=, tvi)散點圖例子散點圖例子7.51234567type= piris, 1iris, 7.51234567type= liris, 1iris, 7.51234567type= biris, 1iris, 7.51234567type= oiris, 1iris, 3 n - 10000 c1 - matrix(rnorm(n, mean = 0, sd = 0.5), ncol = 2) c2 - matrix(rnorm(n, mean = 3, sd = 2), ncol = 2) mydata -

15、rbind(c1, c2) mydata - as.data.frame(mydata) names(mydata) - c(x, y) with(mydata, plot(x, y, pch = 19, main = Scatter Plot with 10,000 Observations) with(mydata, smoothScatter(x, y, main = Scatterplot Colored by Smoothed Densities)高密度的散點圖高密度的散點圖-50510-50510Scatter Plot with 10,000 Observationsxy-505

16、10-50510Scatterplot Colored by Smoothed Densitiesxylibrary(hexbin) with(mydata, bin - hexbin(x, y, xbins = 50) plot(bin, main = Hexagonal Binning with 10,000 Observations) )高密度的散點圖高密度的散點圖( (六邊形封箱法六邊形封箱法) )-50510-50510 xyHexagonal Binning with 10,000 Observations-50510-50510 xyHexagonal Binning with

17、10,000 Observations1173450668399115132148164180197213229246262Counts2.7 等高圖等高圖Contour Plot等高圖或等高線,就是將平面上對應的等高圖或等高線,就是將平面上對應的 z 值值(高度高度)相等的點用顏色或線連接起來。表面上相等的點用顏色或線連接起來。表面上看起來是二維形式,但實際上展示的是三維看起來是二維形式,但實際上展示的是三維數(shù)據(jù)。數(shù)據(jù)。R 中使用中使用 contour()函數(shù)來繪制等高圖。函數(shù)來繪制等高圖。等高圖例子等高圖例子data(volcano)x - 10*(1 : nrow(volcano)y -

18、 10*(1 : ncol(volcano)image(x, y, volcano, col = terrain.colors(100), axes = FALSE) # 產(chǎn)生不同顏色填充的網(wǎng)格產(chǎn)生不同顏色填充的網(wǎng)格contour(x, y, volcano, levels = seq(90, 200, by = 5), add = TRUE, col = peru) # 畫等高線畫等高線xy2.8 三維透視圖三維透視圖(Perspective Plot)三維透視圖是將一個矩陣中包含的高度數(shù)值用曲面連接三維透視圖是將一個矩陣中包含的高度數(shù)值用曲面連接起來而形成的圖形。起來而形成的圖形。R使用使

19、用 persp( )函數(shù)繪制三維透視圖。函數(shù)繪制三維透視圖。 三維透視圖例子三維透視圖例子 z - 3 * volcano x - 10 * (1 : nrow(z) y - 10 * (1 : ncol(z) par(mar = rep(1, 4) persp(x, y, z, theta = 150, phi = 30, col = green3, axes = T, scale = F, ltheta = -120, shade = 0.75, border = NA, box = T)2.9 顏色等高圖顏色等高圖顏色等高圖,又稱為層次圖(顏色等高圖,又稱為層次圖(Level Plot)

20、,與等高圖),與等高圖的原理類似,只是顏色等高圖用不同顏色表示不同高度的原理類似,只是顏色等高圖用不同顏色表示不同高度,并配有顏色圖例,用以說明圖中的顏色與高度值的對,并配有顏色圖例,用以說明圖中的顏色與高度值的對應關系。顏色等高圖對于試驗數(shù)據(jù)的空間分析,可以做應關系。顏色等高圖對于試驗數(shù)據(jù)的空間分析,可以做一個直觀、形象的判斷。一個直觀、形象的判斷。以某松樹某個林分的10年生胸徑為例,試驗林是35行、40列,共1400個數(shù)據(jù)。顏色等高圖例子顏色等高圖例子par(mar = c(4, 4, 2, 2), cex.main = 1)df - read.table( file = d5.1.10.

21、csv, header = F, sep = , )df - as.matrix(df)x = 1 : nrow(df)y = 1 : ncol(df)filled.contour(x, y, df, color = terrain.colors, plot.title = title(main = The Topography of dbh10 in Pinus Loc 8, xlab = Col, ylab = Row), plot.axes = axis(1, seq(5, 40, by = 5) axis(2, seq(5, 35, by = 5) , key.title = titl

22、e(main = DBHn(cm), cex.main = 0.7) )names = c(A, B, C)2.10散點圖矩陣散點圖矩陣(Scatter plot Matrices) 散點圖矩陣是散點圖的高維擴展,它的基本構成是普通散點圖矩陣是散點圖的高維擴展,它的基本構成是普通散點圖,只是將多個變量的兩兩散點圖以矩陣的形式排散點圖,只是將多個變量的兩兩散點圖以矩陣的形式排列起來,就構成了所謂的散點圖矩陣,它通常包含列起來,就構成了所謂的散點圖矩陣,它通常包含 pp 個窗格,個窗格, p 為變量個數(shù)為變量個數(shù) 。散點圖矩陣從一定程度上克服了在平面上展示高維數(shù)據(jù)散點圖矩陣從一定程度上克服了在平面

23、上展示高維數(shù)據(jù)的困難,對于我們查看變量之間的兩兩關系非常有用。的困難,對于我們查看變量之間的兩兩關系非常有用。用用 pairs( ) 函數(shù)產(chǎn)生所有列函數(shù)產(chǎn)生所有列(column)之間兩兩成對的散之間兩兩成對的散點矩陣圖。點矩陣圖。pairs(iris ,1:4) Sepal.Length2.03.04.03.5Sepal.WidthPetal.Length1351234567Petal.Width圖形變換圖形變換1source(panelutils.R) # panelutils.R腳本文件必須與當前R工作空間在同一文件

24、#夾下# 帶頻度分布的柱狀圖和光滑擬合曲線的雙變量散點圖pairs(iris,1:4, panel=panel.smooth, diag.panel=panel.hist,main=雙變量散點圖(帶頻度分布圖和平滑曲線))圖形變換圖形變換2source(panelutils.R) # panelutils.R腳本文件必須與當前R工作空間在同一文件#夾下# 帶頻度分布的柱狀圖和光滑擬合曲線的雙變量散點圖pairs(iris,1:4, lower.panel=panel.smooth, upper.panel=panel.cor, diag.panel=panel.hist,main=雙變量散點圖

25、(帶頻度分布圖和平滑曲線))2.11 條件圖條件圖(Conditioning plot)coplot( )函數(shù)可以畫出條件圖。函數(shù)可以畫出條件圖。coplot 函數(shù)能夠說明函數(shù)能夠說明數(shù)據(jù)集中變量的三向甚至四向關系,它特別于適合觀察數(shù)據(jù)集中變量的三向甚至四向關系,它特別于適合觀察當給定其他預測變量時,響應變量如何根據(jù)一個預測變當給定其他預測變量時,響應變量如何根據(jù)一個預測變量變化。量變化。條件圖例子條件圖例子df-read.table(file=d5.1.6.csv,header=T,sep=,)#單個條件變量的條件圖單個條件變量的條件圖coplot(dbh h|as.factor(str),

26、 data = df)#雙條件變量的條件圖雙條件變量的條件圖coplot(dbh h|as.factor(str) + as.factor(dis), data = df)10251618202216182022161820221025hdbh123468Given : as.factor(str)102030162216221622162216221622102030hdbh123468Given : as.factor(str)01Given : as.factor(dis)2.12相關圖相關圖(Correlograms) 當一個數(shù)據(jù)集中的變量數(shù)目比較多且數(shù)值很多時,計算當一個數(shù)據(jù)集中的變

27、量數(shù)目比較多且數(shù)值很多時,計算相關系數(shù)矩陣是多元統(tǒng)計分析的一個基本方法,但哪些相關系數(shù)矩陣是多元統(tǒng)計分析的一個基本方法,但哪些變量相對獨立,哪些變量之間相關程度最大?變量之間變量相對獨立,哪些變量之間相關程度最大?變量之間是否以某種特定的方式聚集在一起?是否以某種特定的方式聚集在一起?corrgram 包的包的 corrgram ()函數(shù)就可將相關系數(shù)矩陣可函數(shù)就可將相關系數(shù)矩陣可視化,其使用格式如下:視化,其使用格式如下: corrgram (x, type =, order =, panel =, text.panel =, diag.panel = ) 相關圖例子相關圖例子df- rea

28、d.csv(file = d.csv, header = T)df.cor-cor(df,-1)library(corrgram)corrgram(df.cor, type=cor,order=T, lower.panel=panel.shade, upper.panel=panel.pie, text.panel=panel.txt, main=Correlogram of fir intercorrelations)lrttlcprowdtwhvdbhwproCorrelogram of fir intercorrelationshdbhvcprowdwprotltwlrtC

29、orrelogram of fir intercorrelations相關圖例子相關圖例子library(corrgram) corrgram(df.cor, type=cor,order=F, lower.panel=panel.conf, upper.panel=panel.pie, text.panel=panel.txt, main=Correlogram of fir intercorrelations)lrt0.79tl0.20 0.18cpro-0.00-0.090.22wd-0.470.16-0.08-0.08tw-0.17-0.20-0.43-0.29-0.04h-0.12-

30、0.20-0.43-0.38-0.110.90v-0.07-0.18-0.41-0.35-0.150.86 0.98dbh0.01 0.02-0.25-0.98-0.020.32 0.43 0.39wproCorrelogram of fir intercorrelations2.13 地圖地圖map library(maps) map(world, fill = TRUE, col = rainbow(200), ylim = c(-60, 90), mar = c(0, 0, 0, 0) title(世界地圖世界地圖)世界地圖library(maps)library(mapdata)map

31、(china, col = black, ylim = c(18, 54), panel.first = grid()title( 中國地圖中國地圖) 中國地圖 #par(mar=c(0, 0, 0, 0) library(maptools) x.map-readShapePoly(bou2_4p.shp) #plot(x.map) plot(x.map,col=rainbow(30)從網(wǎng)站下載中國地圖的從網(wǎng)站下載中國地圖的GIS數(shù)據(jù)并解壓為三個文件數(shù)據(jù)并解壓為三個文件 (下載網(wǎng)址:下載網(wǎng)址: /wp-content/uploads/2009/07/chinapro

32、vinceborderdata_tar_gz.zip),將這三個文件拷貝到將這三個文件拷貝到R中設好的工作空間下,然后運行代碼:中設好的工作空間下,然后運行代碼: getColor=function(mapdata,provname,provcol,othercol) f=function(x,y) ifelse(x %in% y,which(y=x),0); colIndex=sapply(mapdatadata$NAME,f,provname); fg=c(othercol,provcol)colIndex+1; return(fg);provname=c(北京市北京市,天津市天津市,上海

33、市上海市,重慶市重慶市)provcol=c(red,green,yellow,purple)plot(x.map,col=getColor(x.map,provname,provcol,white) library(ggmap) library(mapproj) # Google獲取獲取China的地圖數(shù)據(jù)的地圖數(shù)據(jù) map - get_map(location = China, zoom = 4) ggmap(map) #Google獲取獲取Guangzhou的公路地圖數(shù)據(jù)的公路地圖數(shù)據(jù) map - get_map(location = Guangzhou, zoom = 10, mapty

34、pe = roadmap) ggmap(map)2.14 lattice包繪圖包繪圖library(lattice) xyplot(uptakeconc|Type*Treatment, data= CO2, group=Plant, type=o, main = Distribution of CO2 uptake by plant, xlab = CO2 conc, ylab = CO2 uptake, auto.key=list(space=right, columns=1, title=Plant) )2.15 ggplot2 包繪圖包繪圖library(ggplot2) #可以將圖作為

35、對象進行運算累加可以將圖作為對象進行運算累加 df - read.table( file= ggplot.pdata.csv, header = T, sep = , )for( i in 1:3 ) df ,i - as.factor(df ,i) str(df)names(df)(p - ggplot( data = df, aes(x = dj , y = dbh10) + geom_point() # 圖圖1(p1-ggplot(df, aes(x=dj , y = dbh10, shape =Spacing, colour = Spacing)+geom_point() # 圖圖2(

36、p2 - p1 + stat_smooth() # 圖圖3(p3 - p1 + geom_point(aes( size = dj ) # 圖圖4(p4 - p3 + scale_size_continuous( range = c(1, 10 ) # 圖圖5(p5 - p1 + facet_wrap( Spacing, ncol = 1 ) )# 圖圖6100150200250300250300350400450djdbh10100150200250300250300350400450djdbh10Spacing23100200300250300350400450djdbh10Spacing

37、23100150200250300250300350400450djdbh10dj250300350400450Spacing23100150200250300250300350400450djdbh10dj250300350400450Spacing2323100150200250300100150200250300250300350400450djdbh10Spacing2343練習三:熟悉練習三:熟悉R繪圖繪圖讀入數(shù)據(jù)讀入數(shù)據(jù)stu.data.csv為數(shù)據(jù)集為數(shù)據(jù)集df1.基礎繪圖基礎繪圖 自己選擇變量分別繪制條形圖、餅圖、扇形圖、直方自己選擇變量分別繪制條形圖、餅圖、扇形圖、直方圖、核

38、密度圖、散點圖、熱圖、散點圖矩陣、條件圖和相圖、核密度圖、散點圖、熱圖、散點圖矩陣、條件圖和相關圖。關圖。2. 高級繪圖高級繪圖 自己選擇變量分別采用自己選擇變量分別采用lattice包和包和ggplot2包繪制圖形。包繪制圖形。練習三練習三 參考代碼參考代碼df-read.csv(file=stu.data.csv,header=T)names(df)# 條形圖條形圖counts-table(df$Grade)par(mfrow = c(1, 2)barplot(counts, xlab=Grade, ylab=Frequency) barplot(counts,xlab=Grade, yl

39、ab=Improvement,horiz=T)counts - table(df$Grade,df$Sex)barplot(counts,main=Stacked Bar Plot, legend=rownames(counts), xlab=Sex, ylab=Frequency,ylim=c(0,100), col=c(red, yellow,green,blue)barplot(counts,main=Grouped Bar Plot, legend=rownames(counts), xlab=Treatment, ylab=Frequency,ylim=c(0,75), col=c(

40、red, yellow, green,blue),beside=T)# 餅圖、扇形圖餅圖、扇形圖slices - as.vector(counts) lbls - c(1, 2, 3,4)pct - round(slices/sum(slices)*100) lbls2 - paste(lbls, n , pct, %, sep=)pie(slices, labels=lbls2, col=rainbow(length(lbls2), main=list(Pie Chart with Percentages,cex=0.8) )library(plotrix)pie3D(slices, labels=lbls2,explode=0.1,font=2, main=3D Pie Chart ,labelcex=1.0, radius=0.75)fan.plot(slices, labels = lbls2, main=Fan Plot,cex=0.5)# 直方圖直方圖hist(df$height,main=Height)# 核密度圖核密度圖plot(density(df$height),main=Height density)# 散點圖散點圖plot(df$weight, df$height)#熱圖熱圖df.2 - as.matrix(df ,-1:-3)r

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論