--- title: "U10_PlotFigure" author: "Feng-Li Lian" date: "11/21/2020" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ## R Markdown # U10: 繪圖功能與文字 # 繪圖視窗之設定 # plot( ) # # in MS-Windows # windows( ) # curve( ) # in Mac OS # quartz( ) # in UNIX # X11( ) # 繪圖視窗 ```{r} data( cars ) dim(cars) summary( cars ) ``` ```{r} cars ``` ```{r} head( cars ) tail( cars ) ``` ```{r} str( cars ) ``` ```{r} plot ( cars ) ``` ###################################### # # 繪圖視窗之設定 # # pointsize: 文字或符號字體大小 ```{r} windows( width = 4.5, height = 3.3, pointsize = 8 ) ``` # mex: 邊界文字的縮放比 # mar: 下 左 上 右 四個邊界之預留距離 # 下: side=1, 左: side=2, 上: side=3, 右: side=4 ```{r} par( mex = 0.8, mar = c(5, 5, 4, 2) + 0.1 ) ``` ```{r} old.par <- par( mex = 0.9, mar = c(5, 5, 4, 2) + 0.1 ) plot ( cars ) ``` # reset to previous settings ```{r} par( old.par ) # reset to previous settings ``` # another command ```{r} 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: # 繪圖的顏色 ```{r} 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") ``` ```{r} 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: # 下 左 上 右 四個邊界之預留距離 ```{r} plot( 0:10, 0:10, col = 1:10, pch = 16, xlab = "X axis", ylab = "Y axis", axes = FALSE ) ``` ```{r} 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] ) ``` ```{r} 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) ) ``` ```{r} 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) ) ``` ```{r} 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: # 在邊界加入文字 ```{r} 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 # 加入文字 – 用滑鼠 ```{r} 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 ) ``` # 加入數學符號 ```{r} 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 ```{r} 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 ```{r} 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 ```{r} # ?plotmath # demo( plotmath ) # on Console Windows # ``` # 字型之圖形參數 # font: # 文字及符號之字型 # font.axis: # 座標軸數字,文字及符號之字型 # font.lab: # 座標軸標記之字型 # font.mian: # 主標題文字及符號之字型 # font.sub: # 副標題文字及符號之字型 ```{r} # demo( Hershey ) # on Console Windows # ```