R軟件學習筆記-5(R軟件畫圖)

轉自:http://www.dataguru.cn/article-2421-1.html

摘要
: 一、直方圖 繪製直方圖函數:hist()對x1進行直方圖分析 hist(x$x1)二、散點圖 散點圖繪製函數:plot()探索各科成績的關聯關係 plot(x1,x2) plot(x$x1,x$x2)三、柱狀圖 列聯表分析 列聯函數table():統計每個分數的人 ...

一、直方圖

繪製直方圖函數:hist()

對x1進行直方圖分析

> hist(x$x1)>


二、散點圖

散點圖繪製函數:plot()

探索各科成績的關聯關係

> plot(x1,x2)> plot(x$x1,x$x2)>


三、柱狀圖

列聯表分析

列聯函數table():統計每個分數的人數;

柱狀圖繪製函數:barplot()

> table(x$x1)  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  1   5   6   6   5   7   5   6   2   3   6   2   2   5   4  11   5   4   5 99 100  8   2> barplot(table(x$x1))>


四、餅圖

餅圖繪製函數:pie()

> pie(table(x$x1))>


五、箱尾圖

1、 箱尾圖繪製函數:boxplot()

> boxplot(x$x1,x$x2,x$x3)>


(1) 箱子的上下橫線爲樣本的25%和75%分位數
(2) 箱子中間的橫線爲樣本的中位數
(3) 上下延伸的直線稱爲尾線,尾線的
(4) 盡頭爲最高值和最低值
(5) 異常值

2、爲箱尾圖添加顏色和缺口

添加參數:顏色 col=c(c(“red”,”green”,”blue”)); 缺口:notch=T。

> boxplot(x[2:4],col=c("red","green","blue"),notch=T)>


3、水平放置的箱尾圖

添加參數:horizontal=T

> boxplot(x$x1,x$x2,x$x2,horizontal=T)>


六、星相圖

函數:stars()

1、

> stars(x[c("x1","x2","x3")])>


(1) 每個觀測單位的數值表示爲一個圖形
(2) 每個圖的每個角表示一個變量,字符串類型會標註在圖的下方
(3) 角線的長度表達值的大小

2、扇形圖 (雷達圖)

添加參數:draw.segment=T 控制是否畫扇形;full=T 控制是圓還是半圓。

> stars(x[c("x1","x2","x3")],full=T,draw.segment=T)>


扇形的面積越大,表示值越大。

3、半扇形

> stars(x[c("x1","x2","x3")],full=F,draw.segment=T)>


七、臉譜圖

1、安裝aplpack包


(1) 用五官的寬度和高度來描繪數值
(2) 人對臉譜高度敏感和強記憶
(3) 適合較少樣本的情況

2、其他臉譜圖

安裝TeachingDemos包

八、莖葉圖

繪製莖葉圖函數:stem()

> stem(x$x2)   The decimal point is 1 digit(s) to the right of the |   6 | 4  6 | 77889  7 | 000112222233344  7 | 5556667777777888899999  8 | 00000000001111111222222222222333344  8 | 5555666788999  9 | 0111223  9 | 57 >


上面莖葉圖中:6 | 4 ,表示64分的有一個;6 | 77889 ,表示67分有兩位同學,68分的有兩位同學,69分的有一位同學。後面以此類推。

九、QQ圖

函數:qqnorm(),qqline()

(1) 可用於判斷是否正態分佈
(2) 直線的斜率是標準差,截距是均值
(3) 點的散佈越接近直線,則越接近正態分佈

> qqnorm(x3)> qqline(x3)>


十、散點圖的進一步設置

1、 plot(x$x1,x$x2,
main=”數學分析與線性代數成績的關係”,
xlab=”數學分析”,
ylab=”線性代數”,
xlim=c(0,100),
ylim=c(0,100),
xaxs=”i”, #Set x axis style as internal
yaxs=”i”, #Set y axis style as internal
col=”red”, #Set the color of plotting symbol to r
pch=19) #Set the plotting symbol to filled dots

2、連線圖

添加參數:type=”l”

> a=c(2,3,4,5,6)> b=c(4,7,8,9,12)> plot(a,b,type="l")>


3、多條曲線的效果

plot(rain$Tokyo,type=”l”,col=”red”,
ylim=c(0,300),
main=”Monthly Rainfall in major cities”,
xlab=”Month of Year”,
ylab=”Rainfall (mm)”,
lwd=2 #線框大小粗細)
lines(rain$NewYork,type=”l”,col=”blue”,lwd=2)
lines(rain$London,type=”l”,col=”green”,lwd=2)
lines(rain$Berlin,type=”l”,col=”orange”,lwd=2)

十一、密度圖

函數:density()

> plot(density(rnorm(1000)))>


十二、熱力圖

1、R內置的數據集,使用函數data()列出。

> data()> mtcars                     mpg cyl  disp  hp drat    wt  qsec vs am gear carbMazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2>

2、利用內置的mtcars數據集繪製

首先要將數據框轉換爲矩陣。顏色越深,數值越大。

heatmap(as.matrix(mtcars),
Rowv=NA,
Colv=NA,
col = heat.colors(256),
scale=”column”,
margins=c(2,8),
main = “Car characteristics by
Model”)

十三、向日葵散點圖

1、Iris(鳶尾花)數據集

2、向日葵散點圖

函數:sunflowerplot(),參數col設定點的顏色,seg.col設定點發出的射線(代表重合點)的顏色。

(1) 用來克服散點圖中數據點重疊問題
(2) 在有重疊的地方用一朵“向日葵花”的花瓣數目來表示重疊數據的個數

> sunflowerplot(iris[,3:4],col="gold",seg.col="gold")>

十四、散點圖集

1、使用函數pairs()

(1) 遍歷樣本中全部的變量配對畫出二元圖
(2) 直觀地瞭解所有變量之間的關係

> pairs(iris[,1:4])>


2、使用plot()函數

plot(iris[,1:4],
main=”Relationships between
characteristics of iris flowers”,
pch=19,
col=”blue”,
cex=0.9)

3、使用par()函數設置

(1) 利用par( )在同一個device輸出多個散點圖
(2) Par命令博大精深,用於設置繪圖參數,help(par)

par(mfrow=c(3,1)) #在一張device中顯示3行一列的圖形
plot(x1,x2);plot(x2,x3);plot(x3,x1)

十五、關於繪圖參數

1、尋求幫助

(1) help(par)
(2) 有哪些顏色? 使用函數 colors()

2、繪圖設備

dev.cur()
dev.list()
dev.next(which = dev.cur())
dev.prev(which = dev.cur())
dev.off(which = dev.cur())
dev.set(which = dev.next())
dev.new(…)
graphics.off()

3、位置控制參數

(1) mai參數:A numerical vector of the form c(bottom, left, top, right) which gives the margin size specified in inches.

(2) oma參數:A vector of the form c(bottom, left, top, right) giving the size of the outer margins in lines of text.


十六、三維散點圖

安裝scatterplot3d 包

scatterplot3d(x[2:4])

十七、三維作圖

x<-y<-seq(-2*pi, 2*pi, pi/15)
f<-function(x,y) sin(x)*sin(y)
z<-outer(x, y, f)
contour(x,y,z,col="blue")
persp(x,y,z,theta=30, phi=30,expand=0.7,col="lightblue")



十八、調和曲線圖

調和曲線用於聚類判斷非常方便。

自定義函數unison的腳本unison.r:

unison<-function(x){   # x is a matrix or data frame of data   if (is.data.frame(x)==TRUE)      x<-as.matrix(x)   t<-seq(-pi, pi, pi/30)   m<-nrow(x); n<-ncol(x)   f<-array(0, c(m,length(t)))   for(i in 1:m){      f[i,]<-x[i,1]/sqrt(2)      for( j in 2:n){          if (j%%2==0)             f[i,]<-f[i,]+x[i,j]*sin(j/2*t)          else             f[i,]<-f[i,]+x[i,j]*cos(j%/%2*t)      }   }   plot(c(-pi,pi), c(min(f),max(f)), type="n",        main="The Unison graph of Data",        xlab="t", ylab="f(t)")  for(i in 1:m) lines(t, f[i,] , col=i)}


十九、地圖

安裝maps包

map(“state”, interior = FALSE)

map(“state”, boundary = FALSE, col=”red”,add = TRUE)

map(‘world’, fill = TRUE,col=heat.colors(10))

相關R繪圖書籍:《R Graphs Cookbook》

發佈了18 篇原創文章 · 獲贊 43 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章