让折线图的点都是想要的 一页中,左边方指数分布的图,右上边是密度,下边随便
#饼图
slices<-c(10,12,4,16,8)
lbls<-c("US","UK","Australia","Germany","France")
pie(slices,labels=lbls)
#一般不使用3d饼图
Assume we have 7 (odds) individual figures but we put them into a 2 ?? 4 frame which left a blank cell. To make things pretty we can do it like this:
# 拆分屏幕split.screen函数
# split display into two screens(1,2)
split.screen(c(2,1))
## [1] 1 2
# split bottom half in two(3,4)
split.screen(c(1,2),2)
## [1] 3 4
# activate screen (1), the top one and plot something
screen(1)
plot((1:10)^2,type="l",main="Screen(1)")
# activate screen (3), the top one and plot something
screen(3)
plot((1:10)^.5,type="l",main="Screen(3)")
# activate screen (4), the top one and plot something
screen(4)
plot((1:10),type="l",main="Screen(4)")
# exit split-screen mode
close.screen(all = TRUE)
Math symbols & formula in graphics Sometimes it is necessary to add mathematical formula in the graphics body. See “demo(plotmath)”
# 所有数学符号的展示方式
# demo(plotmath)
# make a hist but use the desity
hist(rnorm(100),xlim=c(-3,3),freq=FALSE)
# add a normal curve here
# ’expr’ must be a function or an expression containing ’x’
x<-seq(-5,5,.01)
curve(dnorm(x),add=TRUE)
# add a density expression
text(x=2, y=.35,
expression(paste("f(x)=",frac(1,sqrt(2*pi)*sigma),
exp(-frac(1,2*sigma^2)*((x-mu)^2)))))
split.screen(c(1,2))
## [1] 1 2
x = seq(from=0,to=2,0.01)
fx = exp(x)
screen(1)
set.seed(250)
hist(rexp(20),main = 'Screen(1)')
screen(2)
split.screen(c(2,1),2)
## [1] 3 4
plot(fx,dexp(fx),type = 'l',main = 'Screen(2)')
text(x = 5, y = .3, # 坐标是图上的坐标
expression(paste("f(x)=",lambda,e^(-lambda*x),',',x>0)))
# 让长句子自动换行
# 函数,,,忘记了QAQ
Legend in graphics Adding a legend is a easy thing. You can define color, type, position etc yourself. Look at this example
x<-seq(0,1,.01)
par(lwd=2)
plot(x,dbeta(x,5,1),type="l", ylim=c(0,3),
col="green", lty=1,xlab="",ylab="")
lines(x,dbeta(x,.5,.5), col="red",lty=2)
lines(x,dbeta(x,1,3),col="blue",lty=3)
lines(x,dbeta(x,2,2), col="pink",lty=4)
lines(x,dbeta(x,2,5), col="black",lty=5)
lines(x,dbeta(x,1,1),col="orange",lty=6)
legend("top",
c("Beta(5,1)","Beta(.5,.5)","Beta(1,3)",
"Beta(2,2)","Beta(2,5)","Beta(1,1)"),
lty=c(1,2,3,4,5,6),
col=c("green","red","blue","pink","black","orange"),
ncol=2)
Feng Li (SAM.CUFE.EDU.CN) S
ts 函数可以将数据转化为时间序列数据
# prepare the monthly time series data
# (8 series), from the Jan, 1961
z <- ts(matrix(rt(200 * 8, df = 3), 200, 8),
start = c(1961, 1), frequency = 12)
head(z)
## Series 1 Series 2 Series 3 Series 4 Series 5 Series 6
## [1,] 1.2690714 2.3173641 2.47728021 0.01736058 -0.9600404 -1.1551066
## [2,] -0.3608242 0.3803142 0.06746924 -0.13847259 -2.1468845 -1.9913884
## [3,] -1.4213408 1.3373458 0.16569593 -0.29446395 -0.1203887 -0.3095159
## [4,] -0.7896441 0.5475267 1.02232487 -1.04431387 -0.2353654 0.9216648
## [5,] 1.1661252 1.2167266 -1.85896488 -0.80459159 1.6622460 0.1054963
## [6,] -0.6469834 -0.9514953 0.44837450 -0.26496622 -1.0204070 0.2922693
## Series 7 Series 8
## [1,] 0.3574738 1.22197282
## [2,] 0.6206585 1.17947939
## [3,] 0.8573703 -0.04072932
## [4,] -0.3082522 -1.14172453
## [5,] -0.4851866 0.05454807
## [6,] -0.9909864 1.56580866
# we can plot some of them for a
# period (1961.1-1969.12) in multiple frames
# window (窗宽)
z1 <- window(z[,1:3], end = c(1969,12))
plot(z1, type = "b")
# or we can plot them in a single frame
plot(z, plot.type="single", lty=1:3, col=4:2)
# the lag opeator plot
# we use the monthly mean relative sunspot
# numbers from 1749 to 1983 in "sunspots"
# x_{t-1} aginst x_t
plot(lag(sunspots, 1), sunspots, pch = ".")
# the acf and pacf
# use the data "Luteinizing Hormone in Blood Samples"
acf(lh)
pacf(lh)
# set the seed
set.seed(125)
x<-1:200
# simulate an AR(1) model
y<-cumsum(rnorm(200))
# calculate the 95% level confident interval
int1<-y-1.96
int2<-y+1.96
# plot the data
plot(y,xlab="t",ylab=expression(y[t]))
# show the band
# 上下95%的置信带
# 多边形函数polygon
polygon(c(x, rev(x)), c(int1, rev(int2)),
col="grey",border=NA)
# plot the line again
lines(y,type="l",col="blue",lwd=2)
不可交互
x <- seq(-10, 10, .3)
y <- x
# 构建三维函数
rotsinc <- function(x,y)
{
sinc <- function(x) { y <- sin(x)/x ; y[is.na(y)] <- 1; y }
sinc( sqrt(x^2+y^2))
}
# outer函数是输出全部结果
z <- outer(x, y, rotsinc)
# 绘制3d图的persp函数
persp(x, y, z, theta = 30, phi = 30, expand = 0.5,
col = "lightgreen", ltheta = 120, shade = 0.75,
ticktype = "detailed",
xlab = "X", ylab = "Y", zlab = "Z")
title(main = expression(z == Sinc(sqrt(x^2 + y^2))))
高度用颜色标注出来 Project 3D data onto 2D with different colors Recall the sinc function we have tried. We can plot them in a 2D picture with color by using the “image” function.
x<-seq(-10,10,.3)
y<-x
# 依然先定义二维函数
rotsinc <- function(x,y)
{
sinc <- function(x) { y <- sin(x)/x ; y[is.na(y)] <- 1; y }
sinc( sqrt(x^2+y^2))
}
z<-outer(x, y, rotsinc) # the rotsinc is defined in section 2.6
image(x, y, z, col=rainbow(60,start=.7,end=.1))
filled.contour(x,y,z) # show the color legend
# 一定要有合适的图例!
Here is another way to disply 3D data in a 2D graph
N <- 50
x <-y<- seq(-1, 1, length=N)
xx <- matrix(x, nrow=N, ncol=N)
yy<-t(xx)
z <- 1 / (1 + xx^2 + (yy + .5 * sin(5*yy))^2)
# here is the contour plot
contour(x, y, z, main = "Contour plot")
x<-seq(-10,10,.3)
# 依然先定义二维函数
rotsinc <- function(x,y)
{
sinc <- function(x) { y <- sin(x)/x ; y[is.na(y)] <- 1; y }
sinc( sqrt(x^2+y^2))
}
z<-outer(x, y, rotsinc)
# 载入rgl包
library(rgl)
## Warning: package 'rgl' was built under R version 3.4.4
plot3d(x,y,z,col="red", size=5)
#在rmd中无法输出为html QAQ