R Markdown

U10: 繪圖功能與文字

繪圖視窗之設定

plot( )

in MS-Windows

windows( )

curve( )

in Mac OS

quartz( )

in UNIX

X11( )

繪圖視窗

data( cars )
dim(cars)
## [1] 50  2
summary( cars )
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
cars
##    speed dist
## 1      4    2
## 2      4   10
## 3      7    4
## 4      7   22
## 5      8   16
## 6      9   10
## 7     10   18
## 8     10   26
## 9     10   34
## 10    11   17
## 11    11   28
## 12    12   14
## 13    12   20
## 14    12   24
## 15    12   28
## 16    13   26
## 17    13   34
## 18    13   34
## 19    13   46
## 20    14   26
## 21    14   36
## 22    14   60
## 23    14   80
## 24    15   20
## 25    15   26
## 26    15   54
## 27    16   32
## 28    16   40
## 29    17   32
## 30    17   40
## 31    17   50
## 32    18   42
## 33    18   56
## 34    18   76
## 35    18   84
## 36    19   36
## 37    19   46
## 38    19   68
## 39    20   32
## 40    20   48
## 41    20   52
## 42    20   56
## 43    20   64
## 44    22   66
## 45    23   54
## 46    24   70
## 47    24   92
## 48    24   93
## 49    24  120
## 50    25   85
head( cars )
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10
tail( cars )
##    speed dist
## 45    23   54
## 46    24   70
## 47    24   92
## 48    24   93
## 49    24  120
## 50    25   85
str( cars )
## 'data.frame':    50 obs. of  2 variables:
##  $ speed: num  4 4 7 7 8 9 10 10 10 11 ...
##  $ dist : num  2 10 4 22 16 10 18 26 34 17 ...
plot ( cars )

###################################### # # 繪圖視窗之設定 # # pointsize: 文字或符號字體大小

windows( width = 4.5, height = 3.3, pointsize = 8 )

mex: 邊界文字的縮放比

mar: 下 左 上 右 四個邊界之預留距離

下: side=1, 左: side=2, 上: side=3, 右: side=4

par( mex = 0.8, mar = c(5, 5, 4, 2) + 0.1 )
old.par <- par( mex = 0.9, mar = c(5, 5, 4, 2) + 0.1 )

plot ( cars )

# reset to previous settings

par( old.par )      # reset to previous settings

another command

win.graph( width = 4.5, height = 3.3, pointsize = 8 )

old.par <- par( mex = 0.8, mar = c(5, 5, 4, 2) + 0.1 )

plot( cars )

par( old.par )      # reset to previous settings

常用的圖形參數

xlim: # x 軸(橫)之範圍; xlim[1]: 左邊界,xlim[2]: 右邊界

ylim: # y 軸(縱)之範圍; ylim[1]: 下邊界,ylim[2]: 上邊界

xlab: # x 軸(橫)之名稱,出現在橫軸下方

ylab: # y 軸(縱)之名稱,出現在橫軸左方

main: # 整個圖形的之主標題,出現在圖形的上方

sub: # 副標題,出現在橫軸下方

cex: # 文字及符號相對於內定值之縮放比

pch: # 點的型式

col: # 繪圖的顏色

plot( cars, xlim = c(0, 30), ylim = c(0, 130), xlab = "xlab", ylab = "ylab", main = "main title", sub = "subtitle", cex = 0.8, pch = 16, col = "red")

plot( 1:25, pch = 0:25, col = 1:8, xlab = "number", ylab = "" )

references

Graphical Parameters

http://www.statmethods.net/advgraphs/parameters.html

R Graphical Parameters Cheat Sheet

http://gastonsanchez.com/visually-enforced/resources/2015/09/22/R-cheat-sheet-graphical-parameters/

座標軸及邊界

axis: # 設定座標軸之函數

axes: # axes = TRUE : 要畫座標軸; axes = FALSE : 不要畫座標軸

xaxt: # xaxt = “n” : 不要畫 x 軸

yaxt: # yaxt = “n” : 不要畫 y 軸

col.axis: # 座標軸之文字顏色

side: # 繪圖區域邊界的編號

mex: # 邊界文字之縮放比

mar: # 下 左 上 右 四個邊界之預留距離

plot( 0:10, 0:10, col = 1:10, pch = 16, xlab = "X axis", ylab = "Y axis", axes = FALSE )

plot( 0:10, 0:10, col = 1:10, pch = 16, xlab = "X axis", ylab = "Y axis", axes = FALSE )
axis( side = 1, col = "black", col.axis = "red", at = 0:10, label = LETTERS[1:11] )

plot( 0:10, 0:10, col = 1:10, pch = 16, xlab = "X axis", ylab = "Y axis", axes = FALSE )

axis( side = 2, col = "green", col.axis = "blue", at = seq(from = 0, to = 10, by = 1) )

plot( 0:10, 0:10, col = 1:10, pch = 16, xlab = "X axis", ylab = "Y axis", axes = FALSE )

axis( side = 3, col = "cyan", col.axis = "magenta", at = seq(from = 0, to = 8, by = 1) )

plot( 0:10, 0:10, col = 1:10, pch = 16, xlab = "X axis", ylab = "Y axis", axes = FALSE )

axis( side = 4, col = "yellow", col.axis = "gray", at = seq(from = 2, to = 10, by = 2), label = c(20, 40, 60, 80, 100) )

加入文字

text: # 加入文字

title: # 加入註解

legend: # 加入圖標,圖例,備註

mtext: # 在邊界加入文字

plot( cars, xlab = "", ylab = "" )

title( main = "cars data", xlab = "speed", ylab = "distance" )

text( 20, 100, label = "text here" )

legend( "topleft", legend = c("x: speed", "y: distance") )

mtext( text = c("Bottom", "Left", "Top", "Right"), side = 1:4, col = 1:4, line = -2 )       

# line = -2: 往內移動兩個線寬度

legend 位置:

topleft, top, topright

left, center, right

bottomleft, bottom, bottomright

加入文字 – 用滑鼠

plot( cars, xlab = "", ylab = "" )

title( main = "cars data", xlab = "speed", ylab = "distance" )

text( 20, 100, label = "text here" )

# legend( locator(1), legend = c("x: speed", "y: distance") )
# on Console Windows #

mtext( text = c("Bottom", "Left", "Top", "Right"), side = 1:4, col = 1:4, line = -2 )

加入數學符號

plot(0:10, 0:10, xlab = "", ylab = "", pch = 1:10, col = 1:10)

text( 2, 8, label = expression( chi^2 == "4.4" ) )

text( 2, 7, label = expression( x[3] == "5.5" ) )

text( 2, 6, label = expression( y[3]^2 == "8.8" ) )

text( 8, 5, label = expression( hat(y) %+-% z ) )

define a new math experession

plot(0:10, 0:10, xlab = "", ylab = "", pch = 1:10, col = 1:10)

label.eq <- expression( paste( "f(x) = ", frac(1, sigma*sqrt(2 * pi)), " ", e^{frac(-(x - mu)^2, 2*sigma^2)} ) )

text( 7, 2, label = label.eq )

define a series of data

x <- seq( from = -4, to = 4, length = 101 )

xlab <- expression( paste( "phase angle ", phi ) )

ylab <- expression( paste( "cos(", phi, ")" ) )

plot( x, cos(x), type = "l", xaxt = "n", xlab = xlab, ylab = ylab )

label <- expression( -pi, -pi / 2, 0, pi / 2, pi )

axis( side = 1, at = c(-pi, -pi / 2, 0, pi / 2, pi), label = label )

demo

# ?plotmath

# demo( plotmath )

# on Console Windows #

字型之圖形參數

font: # 文字及符號之字型

font.axis: # 座標軸數字,文字及符號之字型

font.lab: # 座標軸標記之字型

font.mian: # 主標題文字及符號之字型

font.sub: # 副標題文字及符號之字型

# demo( Hershey )

# on Console Windows #