
# 多張圖形 – mfrow, mfcol

old.par <- par( mfrow = c(2, 2), mex = 0.8, mar = c(5, 5, 4, 2) + 0.1 )
plot( cars, main = "cars data", cex = 2 )
plot( cars, main = "cars data", cex.axis = 2 )
plot( cars, main = "cars data", cex.lab = 2 )
plot ( cars, main = "cars data", cex.main = 2 )
par( old.par )

# 多張圖形 – las

old.par <- par(mfrow = c(2, 2), mex = 0.8, mar = c(5, 5, 4, 4) + 0.1)
plot( cars, main = "las = 0", las = 0 )
# 0: always parallel to the axis [default]
plot( cars, main = "las = 1", las = 1 )
# 1: always horizontal
plot( cars, main = "las = 2", las = 2 )
# 2: always perpendicular to the axis
plot( cars, main = "las = 3", las = 3 )
# 3: always vertical

par( old.par )

# 多張圖形 – type

old.par <- par( mfrow = c(2, 3), mex = 0.6, mar = c(5, 4, 4, 2) + 0.1 )

plot( cars, type = "p", main = "type = p" )	# "p" for points
plot( cars, type = "l", main = "type = l" )	# "l" for lines
plot( cars, type = "b", main = "type = b" )	# "b" for both
plot( cars, type = "o", main = "type = o" )	# "o" for both ‘overplotted’
plot( cars, type = "h", main = "type = h" )	# "h" for histogram-like vertical lines
plot( cars, type = "n", main = "type = n" )	# "n" for no plotting

par(old.par) 

# 多張圖形之位置安排

layout( matrix( c( 1, 2, 3, 4) , 2, 2, byrow = T ), widths = c(1,1), heights = c(1,1) )
plot( iris[  , 1 ] )
plot( iris[  , 2 ] )
plot( iris[  , 3 ] )
plot( iris[  , 4 ] )

layout( matrix( c( 1, 2, 3, 4) , 2, 2, byrow = T ), widths = c(1,1), heights = c(1,1) )
hist( iris[  , 1 ] )
hist( iris[  , 2 ] )
hist( iris[  , 3 ] )
hist( iris[  , 4 ] )

layout( matrix( c( 1, 2, 3, 4) , 2, 2, byrow = T ), widths = c(1,1), heights = c(1,1) )
plot( iris[  , 1 ] )
plot( iris[  , 2 ] )
plot( iris[  , 3 ] )
plot( iris[  , 4 ] )

layout( matrix( c( 1, 2, 3, 4) , 2, 2, byrow = T ), widths = c(1,1), heights = c(1,1) )
hist( iris[  , 1 ] )
hist( iris[  , 2 ] )
hist( iris[  , 3 ] )
hist( iris[  , 4 ] )

layout( matrix( c( 1, 2, 3, 4, 5, 6, 7, 8) , 4, 2, byrow = T ), widths = c(1,1), heights = c(1,1,1,1) )
plot( iris[  , 1 ] )
hist( iris[  , 1 ] )
plot( iris[  , 2 ] )
hist( iris[  , 2 ] )
plot( iris[  , 3 ] )
hist( iris[  , 3 ] )
plot( iris[  , 4 ] )
hist( iris[  , 4 ] )

layout( matrix( c( 1, 2, 3, 4, 5, 6, 7, 8) , 4, 2, byrow = T ), widths = c(1,2), heights = c(1,1,1,1) )
plot( iris[  , 1 ] )
hist( iris[  , 1 ] )
plot( iris[  , 2 ] )
hist( iris[  , 2 ] )
plot( iris[  , 3 ] )
hist( iris[  , 3 ] )
plot( iris[  , 4 ] )
hist( iris[  , 4 ] )

layout( matrix( c( 1, 2, 3, 4) , 2, 2, byrow = T ), widths = c(2,1), heights = c(1,1) )
plot( iris[  , 1 ] )
hist( iris[  , 1 ] )
plot( iris[  , 2 ] )
hist( iris[  , 2 ] )

layout( matrix( c( 1, 2, 3, 4) , 2, 2, byrow = T ), widths = c(1,1), heights = c(2,1) )
plot( iris[  , 1 ] )
hist( iris[  , 1 ] )
plot( iris[  , 2 ] )
hist( iris[  , 2 ] )

layout( matrix( c( 1, 2, 3, 4) , 2, 2, byrow = T ), widths = c(1,3), heights = c(1,1) )
plot( iris[  , 1 ] )
hist( iris[  , 1 ] )
plot( iris[  , 2 ] )
hist( iris[  , 2 ] )

layout( matrix( c( 1, 2, 3, 4) , 2, 2, byrow = T ), widths = c(1,3), heights = c(1,3) )
plot( iris[  , 1 ] )
hist( iris[  , 1 ] )
plot( iris[  , 2 ] )
hist( iris[  , 2 ] )

layout( matrix( c( 1, 2, 3, 0) , 2, 2, byrow = T ), widths = c(1,1), heights = c(1,1) )
plot( iris[  , 1 ] )
plot( iris[  , 2 ] )
plot( iris[  , 3 ] )

layout( matrix( c( 1, 0, 2, 3) , 2, 2, byrow = T ), widths = c(1,1), heights = c(1,1) )
plot( iris[  , 1 ] )
plot( iris[  , 2 ] )
plot( iris[  , 3 ] )

layout( matrix( c( 1, 0, 0, 2) , 2, 2, byrow = T ), widths = c(1,1), heights = c(1,1) )
plot( iris[  , 1 ] )
plot( iris[  , 2 ] )

# 幾張圖繪製在一起


par( fig=c(   0, 0.8,   0, 0.7 ), new=TRUE )
plot( iris[  , 1 ] )

par( fig=c(   0, 0.8, 0.7,   1 ), new=TRUE )
plot( iris[  , 2 ] )

par( fig=c( 0.8,   1,    0, 0.7 ), new=TRUE )
plot( iris[  , 3 ] )

par( fig=c( 0.8,   1,   0.7, 1  ), new=TRUE )
plot( iris[  , 4 ] )

# 多張圖形 – layout

old.par <- par( mex = 0.8, mar = c(5, 4, 4, 2) + 0.1 )
layout( matrix( c(1, 2, 1, 3), nrow = 2, ncol = 2) )
plot( cars, las = 1, main = "las = 1" )
plot( cars, las = 2, main = "las = 2" )
plot( cars, las = 3, main = "las = 3" )
layout(1)

par(old.par)

old.par <- par( mex = 0.8, mar = c(5, 4, 4, 2) + 0.1 )
layout( matrix( c(1, 2, 1, 3), nrow = 2, ncol = 2) )
plot( cars, las = 1, main = "las = 1" )
plot( cars, las = 2, main = "las = 2" )
plot( cars, las = 3, main = "las = 3" )
layout(1)

par(old.par)

old.par <- par( mex = 0.8, mar = c(5, 4, 4, 2) + 0.1 )
layout( matrix( c(1, 2, 0, 3), nrow = 2, ncol = 2), width = c(2, 1), height = c(1, 1.5))
plot( cars, las = 1, main = "las = 1" )
plot( cars, las = 2, main = "las = 2" )
plot( cars, las = 3, main = "las = 3" )
layout(1)

par(old.par)

# add graphic elements 加入圖形元件

x <- c( 2, 3, 4 )
y <- c( 6, 7, 8 )
#
old.par <- par( mex = 0.8, mar = c(5, 4, 4, 2) + 0.1 )
plot( 0:10, 0:10, xlab = "", ylab = "", pch = 1:11, col = 1:8, axes = FALSE )
axis( side = 1, at = 0:10 )
axis( side = 2, at = 0:10 )
box()
#
lines( c(1, 2), c(9, 10), col = "yellow", lwd = 3 )
abline( a = -2, b = 1, col = "gold", lwd = 2 )
abline( h = 1:3, lty = 1:3, lwd = c(1.0, 1.5, 2.0), col = c("red", "green", "blue") )
abline( v = 6:8, lty = 1:3, lwd = c(1.0, 1.5, 2.0), col = c("red", "green", "blue") )
#
points( x, y, col = "violet", pch = 16, cex = 2 )
segments( x[1], y[1], x[2], y[2], col = "violet", lwd = 2)
segments( x[2], y[2], x[3], y[3], col = "dark violet", lwd = 2 )
arrows( 1, 7, 1, 8, code = 1, length = 0.1, col = "pink", lwd = 2 )
arrows( 1, 7, 1, 8, code = 1, length = 0.1, col = "pink", lwd = 2 )
arrows( 2, 8, 3, 9, code = 2, length = 0.1, col = "pink", lwd = 2 )
par( old.par )

# draw face

old.par <- par( mex = 0.8, mar = c(5, 4, 4, 2) + 0.1 )
plot( 0:10, 0:10, xlab = "", ylab = "", pch = 1:11, col = 1:8, type = "n" )
axis( side = 1, at = 0:10 )
axis( side = 2, at = 0:10 )


# 臉輪廓
symbols( 5, 5, circles = 4, fg = "blue", lwd = 2, inches = FALSE, add = TRUE )

# 眼睛
symbols( 3.5, 6.5, circles = 0.5, fg = "dark violet", bg = "purple", inches = FALSE, add = TRUE )
symbols( 6.5, 6.5, circles = 0.5, fg = "dark violet", bg = "purple", inches = FALSE, add = TRUE )

# 眉毛
symbols( 3.5, 8.5, rectangles = matrix(c(1.5, 0.5), nrow =1), fg = "gray", inches = FALSE, add = TRUE )
symbols( 6.5, 8.5, rectangles = matrix(c(1.5, 0.5), nrow =1), fg = "gray", inches = FALSE, add = TRUE )

# 鼻子
symbols( 5, 4.5, rectangles = matrix(c(0.5, 2), nrow =1), fg = "green", bg = "lightgreen", inches = FALSE, add = TRUE )

# 嘴巴
symbols( 5, 1.5, squares = 1.5, fg = "red", lwd = 2, inches = FALSE, add = TRUE )

# 鬍子
polygon( c(3.5, 3, 2), c(3, 3, 2), col = "orange" )
polygon( c(6.5, 7, 8), c(3, 3, 2), col = "orange" )

par(old.par)

# 呈現多筆數據於一張圖

x <- seq(from = -pi, to = pi, length = 101)

y1 <- sin(x)
y2 <- cos(x)
y3 <- sin(x) + cos(x)

ylim <- range(y1, y2, y3)

# Using matpoints

old.par <- par( mex = 0.8, mar = c(5, 4, 3, 1) + 0.1 )

plot( x, y1, xlim = range(x), ylim = ylim, type = "n", xaxt = "n", xlab = "x", ylab = "", main = "Using matpoints()" )

label <- expression( -pi, -pi / 2, 0, pi / 2, pi )
axis( side = 1, at = c(-pi, -pi / 2, 0, pi / 2, pi), label = label )

matpoints( x, cbind(y1, y2, y3), type = "l", col = c("red", "green", "blue"), lty = 1:3, lwd = 1:3 )

legend( "topleft", legend = c("y1", "y2", "y3"), col = c("red", "green", "blue"), lty = 1:3, lwd = 1:3 )

par(old.par)

# Using matlines

old.par <- par( mex = 0.8, mar = c(5, 4, 3, 1) + 0.1 )
plot( x, y1, xlim = range(x), ylim = ylim, type = "n", xaxt = "n", xlab = "x", ylab = "", main = "Using matlines()" )
label <- expression( -pi, -pi / 2, 0, pi / 2, pi )
axis( side = 1, at = c(-pi, -pi / 2, 0, pi / 2, pi), label = label )

matlines( x, cbind(y1, y2, y3), col = c("red", "green", "blue"), lty = 1:3, lwd = 1:3 )

legend( "topleft", legend = c("y1", "y2", "y3"), col = c("red", "green", "blue"), lty = 1:3, lwd = 1:3 )

par(old.par)


# 顏色

old.par <- par( mex = 0.8, mar = c(4, 2, 2, 2) + 0.1 )
barplot( 1:8, col = palette(), names.arg = 1:8, yaxt = "n" )
par( old.par )

# 顏色 – 調色盤

n <- 10

old.par <- par( mfrow = c(2, 3), mex = 0.6, mar = c(5, 4, 4, 2) + 0.1 )

barplot( 1:n, col = colors()[1:n], names.arg = 1:n, yaxt = "n", main = "colors" )
barplot( 1:n, col = rainbow(n), names.arg = 1:n, yaxt = "n", main = "rainbow" )
barplot( 1:n, col = heat.colors(n), names.arg = 1:n, yaxt = "n", main = "heat.colors" )
barplot( 1:n, col = terrain.colors(n), names.arg = 1:n, yaxt = "n", main = "terrain.colors" )
barplot( 1:n, col = topo.colors(n), names.arg = 1:n, yaxt = "n", main = "topo.colors" )
barplot( 1:n, col = cm.colors(n), names.arg = 1:n, yaxt = "n", main = "cm.colors" )

par( old.par )

# 顏色 – 灰階圖

gray.scale <- seq( from = 0, to = 1, length = 10 )

old.par <- par( mex = 0.8, mar = c(4, 2, 2, 2) + 0.1 )
barplot( 1:10, col = gray(gray.scale), names.arg = 1:10, yaxt = "n", main = "gray scale" )
par( old.par )

# 顏色 – 自行定義調色盤

palette( terrain.colors(20) )  		# redefine palette
palette( )
old.par <- par( mex = 0.8, mar = c(4, 2, 2, 2) + 0.1 )
barplot( 1:20, col = 1:20, names.arg = 1:20, yaxt = "n" )

par(old.par)

old.par <- par( mex = 0.8, mar = c(4, 2, 2, 2) + 0.1 )
plot( 1:20, pch = 16, cex = seq(from = 1, to = 10, length = 20), col = 1:20, xlab = "" )
par(old.par)
palette( "default" )  		# using default colors
palette()

old.par <- par( mex = 0.8, mar = c(4, 2, 2, 2) + 0.1 )

plot( 1:20, pch = 16, cex = seq(from = 1, to = 10, length = 20), col = 1:20, xlab = "" )
par(old.par)
palette( "default" )  		# using default colors

palette()

# 顏色有關的指令

library( DAAG )
show.colors( type = "singles", order.cols = TRUE )
show.colors( type = "shades", order.cols = TRUE )
show.colors( type = "gray", order.cols = TRUE )

# 使用顏色圖形參數

old.par <- par( bg = "lightyellow", col.main = "navy", col.lab = "magenta", mex = 0.8, mar = c(5, 5, 4, 3) + 0.1 )

plot( 0:10, 0:10, pch = 16, col = rainbow(10), main = "Main title", xlab = "X axis", ylab = "Y axis", axes = FALSE )

axis( side = 1, col = "black", col.axis = "red", at = 0:10, label = LETTERS[1:11] )
axis( side = 2, col = "green", col.axis = "blue", at = seq(from = 0, to = 10, by = 1) )
axis( side = 3, col = "cyan", col.axis = "magenta", at = seq(from = 0, to = 8, by = 1) )
axis( side = 4, col = "yellow", col.axis = "gray", at = seq(from = 2, to = 10, by = 2), label = c(20, 40, 60, 80, 100) )
mtext( text = c( "Bottom", "Left", "Top", "Right" ), side = 1:4, col = 1:4, line = -2 )

par(old.par)

# 數學函數繪圖

# 1 function, 1 graph

f <- function(x)  0.01 * x^3 * cos(x) - 0.2 * x^2 * sin(x) + 0.05 * x - 1

old.par <- par( mex = 0.8, mar = c(5, 5, 4, 2) + 0.1 )

curve( f, from = -10, to = 10 )
curve( f, from = -10, to = 10, n = 1001 )
curve( f, from = -10, to = 10, n = 11 )
curve( f, from = -10, to = 10, n = 101 )
curve( f, from = -10, to = 10, n = 1001 )

par( old.par )

# 2 functions, 2 graphes

f <- function(x)  0.01 * x^3 * cos(x) - 0.2 * x^2 * sin(x) + 0.05 * x - 1
g <- function(x)  10 * cos(x) * sin(x)

old.par <- par( mex = 0.8, mar = c(5, 5, 4, 2) + 0.1 )

curve( f, from = -10, to = 10, lty = 1, col = "red", ylab = "" )
curve( g, add = TRUE, lty = 2, col = "blue" )

legend( "topright", legend = c("f", "g"), lty = 1:2, col = c("red", "blue"))

par(old.par)






