第4.3节

回顾

上节课讲了似然函数 随着参数变化,函数值也变化 无图无真相,一图胜千言 R Graphic 画图的步骤:画图,输出

##SCREEN/GUI devices
x11()
windows()
quartz()

## file devices
postscript()
pdf()
pictex()
xfig()
bitmap()
png()
jpeg()
## divices provided by add-on packages
devGTK()
devJava()
devSVG()

基本的画图函数

par(mfrow = c(1,2))
## plot
x = seq(-5,5,0.01)
fx = dnorm(x,0,1)
plot(x,fx)
#### axis 刻度
#### horizontal axis 横坐标
#### vertical axis 纵坐标
#### curve 曲线
#### frame 框
### 加x_label标签
plot(x,fx,xlab = 'random numbers')

plot(x,fx,xlab = 'random numbers',ylab = 'density of normal dist', 
     main = 'density plot')  # 标题,纵坐标标题
plot(x,fx,xlab = 'random numbers',ylab = 'density of normal dist', 
     main = 'density plot',col = 'steelblue')  # 颜色

plot(x,fx,xlab = 'random numbers',ylab = 'density of normal dist', 
     main = 'density plot',col = 'steelblue', type = 'l') # line变点为线
plot(x,fx,xlab = 'random numbers',ylab = 'density of normal dist', 
     main = 'density plot',col = 'steelblue', type = 'b') # both添加一条线而不去掉散点

plot(x,fx,xlab = 'random numbers',ylab = 'density of normal dist', 
     main = 'density plot',col = 'steelblue', type = 'b',lwd = 3) # 线宽

x2 = rnorm(1000,0,1)
fx2 = dnorm(x,0,1)

排序函数

par(mfrow = c(1,2))
sort(c(1,9,3,5))
## [1] 1 3 5 9
order(c(1,9,3,5))
## [1] 1 3 4 2
plot(sort(x2),fx2[order(x2)],xlab = 'random numbers',ylab = 'density of normal dist', 
     main = 'density plot',col = 'steelblue', type = 'b')

plot(x,fx,xlab = 'random numbers',ylab = 'density of normal dist', 
     main = 'density plot',col = 'steelblue', type = 'b',lwd = 3,xlim = c(-2,0)) # 限定范围

切割/拼接图形

par(mfrow = c(1,2))
par(mfrow = c(1,1))
par(mfcol = c(2,1))

输出图片(暂时不要运行???调试中)

setwd('C:/Users/wangyuanhe/Desktop')
dev.copy2pdf(filename = '1.pdf')
dev.copy(device = x11)

plot(x,fx,xlab = 'random numbers',ylab = 'density of normal dist', 
     main = 'density plot',col = 'steelblue', type = 'b',lwd = 1,xlim = c(-2,0)) # 限定范围

#### png(filename = '1.png')
#### detach()

调整标题位置,adj

par(mfrow=c(2,2))
par(adj=0)
plot(0,main="par(adj=0)",ylab="ylab",xlab="xlab")
par(adj=.5)
plot(0,main="par(adj=.5)default",ylab="ylab",xlab="xlab")
par(adj=.7)
plot(1,main="par(adj=.7)",ylab="ylab",xlab="xlab")
par(adj=1)
plot(1,main="par(adj=1)",ylab="ylab",xlab="xlab")

改变横纵坐标文字方向

par(mfrow=c(2,2)) # we plot them in a picture
par(las=0)
plot(0,main="par(las=0) default")
par(las=1)
plot(0,main="par(las=1)")
par(las=2)
plot(0,main="par(las=2)")
par(las=3)
plot(0,main="par(las=3)")

设定封口的线

par(mfrow=c(2,4))
par(bty="o")
plot(0,main="par(bty=\"o\")")
par(bty="l")
plot(0,main="par(bty=\"l\")")
par(bty="7")
plot(0,main="par(bty=\"7\")")
par(bty="n")
plot(0,main="par(bty=\"n\")")
par(bty="c")
plot(0,main="par(bty=\"c\")")
par(bty="u")
plot(0,main="par(bty=\"u\")")
par(bty="]")
plot(0,main="par(bty=\"]\")")

防止plot被覆盖

x = seq(-5,5,0.1)
fn = dnorm(x)
ft = dt(x,df = 4)
par(mfrow = c(1,1))
plot(x,ft,type = 'l',col = 'blue')
points(x,fn,type = 'l',col = 'red')

### 先绘制fn可以使大的图在范围内
plot(x,fn,type = 'l',col = 'blue')
points(x,ft,type = 'l',col = 'red')

合并图层 fig(怎么取消QAQ)

### give 100 random t with df 12
x <- rt(100, df=12)
### make a histgram
hist( x, col = "light blue")
### prepare to add a new small graph
op <- par(fig = c(.02,.42,.53,.99),new = TRUE)
### the qqnorm in the small graph
qqnorm(x, xlab = "", ylab = "", main = "", axes = FALSE)
### add a qq line in the small graph
qqline(x, col = "red", lwd = 2)
### set the line width of the small frame box
box(lwd=1)

### apply
par(op)

par(mfrow = c(1,1))

双坐标图

### Some economists like double axis graphs. Here how it works in R
set.seed(1) # set the seed
par(mar=c(5,5,5,5)) # set the margin边际
### asumption this is Interest rate
x1<-sort(rnorm(100,mean=20,sd=5))
x2 <- (x1)^3 # this is GDP
### plot GDP
plot(x2,axes=FALSE,type="l",col="blue",xlab="",ylab="")
axis(1) # add the axis on bottom 增加一个坐标,下1
axis(2,col="blue") # add the asis on left 增加一个坐标,左2
par(new=TRUE) # add new plot on the current graph
plot(x1,axes=FALSE,type="l",col="red",xlab="",ylab="")
axis(4,col="red") # add the axis on right 增加一个坐标,右4
### We will give the three axises labels 
mtext(c("Time","GDP(mil. $)","Interest rate(%)"),
      side=c(1,2,4),line=3) # line=3是字体粗细
title("GDP with interest rate") # give the main title

增加网格

par(mfrow=c(2,2))
plot(1:3)
# grid only in y-direction
grid(nx=NA, ny=5,col="gray")
plot(1:3)
# only x-direction
grid(nx=5,ny=NA, col="red")
plot(1:3)
# both x and Y
grid(nx = 5, ny = 5,lty="dashed",col="blue")
plot(1:3)
# x and y but different numbers
grid(nx = 3, ny = 5)