您的位置:首页 > 移动开发 > 微信开发

几个R语言画图小程序分享

2017-08-16 14:02 281 查看
三维图

x=seq(-5,5,by=0.1) #步长很小时画的图就是黑色的了,因为都是画格子的黑线的颜色
y=x
x1=dnorm(x,0,1)  #dnorm()为正态分布密度函数
z=outer(x1,x1)
persp(x,y,z,theta =30,phi = 25,expand = 0.5,col = "Blue2")




等高线图

library(MASS)
Sigma <- matrix(c(1,0.7,0.7,1),2,2)
Sigma
r=mvrnorm(n=1000, c(0,3), Sigma)
par(mfrow = c(2, 2))
#density plot
plot(density(r))
#kernel density estimate
bivn.kde <- kde2d(r[,1], r[,2], n = 50)
#perspective plot
persp(bivn.kde, phi = 45, theta = 30)
#contour plot
contour(bivn.kde)
#contour plot with image
image(bivn.kde,col = terrain.colors(100)); contour(bivn.kde, add = T)




区间概率变化

library(animation)
oopt = ani.options(interval = 0.5)
for (r2 in seq(-4,4,l=20))
{x=c(seq(-4,4,l=1000))
r1=-3
x2=c(r1,r1,x[x<r2&x>r1],r2,r2)
y2=c(0,dnorm(c(r1,x[x<r2&x>r1],r2)),0)
plot(x,dnorm(x),type="l",ylab=expression(phi(x)))
abline(h=0);polygon(x2,y2,col="red")
text(-3,0.3,r2)
ani.pause()
}

ani.options(oopt)




大物实验报告数据分析

x <-c(2.55,2.67,3.00,3.66,3.99,4.73,5.39,6.29,6.94,6.96,7.37,7.69,8.00,8.37,9.51,10.98,12.40)
y <- c(0.84,0.84,0.83,0.80,0.60,0.38,0.06,0.07,0.06,0.05,0.06,0.05,0.04,0.03,0.02,0.01,0)
plot(x,y,pch=20)
lines(x,y)




饼图

x=c(98,99,60,48)
y=c("概率论","数理统计","复变函数","实变函数")
pie(x,labels=y,main="刘青总成绩分布")




两侧分位数

#画t分布密度及双侧分位数
p=0.05
qt(0.025,19)  #已知概率,计算对应的分位数

x=seq(-6,6,length=1000)
y=dt(x,19)

r1=-6
r2=-2.093
r3=2.093
r4=6
#范围
x2=c(r1,r1,x[x<r2&x>r1],r2,r2)
y2=c(0,dt(c(r1,x[x<r2&x>r1],r2),19),0)#对应具体的分布
x3=c(r3,r3,x[x<r4&x>r3],r4,r4)
y3=c(0,dt(c(r3,x[x<r4&x>r3],r4),19),0)#对应具体的分布

plot(x,y,type="l",ylab="Density of t(19)",xlim=c(-5,5))
#画出t的密度函数图像
abline(h=0)
#图颜色
#polygon(x2,y2,col="red")
#polygon(x3,y3,col="red")
polygon(c(x2,x3),c(y2,y3),col="red")

#加标题
title("Tail Probability for t(19)")
text(c(-3.9,-1.3,3.5,4.1),c(0.03,0.01,0.05,0.01),
c("p-value/2=0.05","t=-2.093","p-value/2=0.05","t=2.093"))

#画F(2,2)分布密度及双侧分位数

qf(0.2,2,2)  #已知概率,计算对应的分位数

x<-seq(0,15,length=1000)
y<-df(x,2,2)
plot(x,y,type="l")

r1<-0
r2<-0.111
r3<-9
r4<-15
x2<-c(r1,r1,x[x<r2&x>r1],r2,r2)
y2<-c(0,df(c(r1,x[x<r2&x>r1],r2),2,2),0)        #对应具体的分布
x3<-c(r3,r3,x[x<r4&x>r3],r4,r4)
y3<-c(0,dt(c(r3,x[x<r4&x>r3],r4),2,2),0)         #对应具体的分布

plot(x,y,type="l",ylab="density of F(2,2)")
abline(h=0)
polygon(c(x2,x3),c(y2,y3),col="red")

#加标题
title("Tail Probability for F(2,2)")
text(c(1.9,0.8,9,10),c(0.1,-0.01,0.05,-0.018),
c("p-value/2=0.2","t=-2.093","p-value/2=0.2","t=2.093"))






分位数图

#t(19)的分位数

qt(0.005,19)      #计算分位数

x<-seq(-6,6,length=1000)
y<-dt(x,19)
plot(x,y,type="l",ylab="density of t(19)",xlim=c(-5,5),ylim=c(0,0.5))

r1<--6
r2<--2.8609

x2<-c(r1,r1,x[x<r2&x>r1],r2,r2)           #选择区域
y2<-c(0,dt(c(r1,x[x<r2&x>r1],r2),19),0)   #要对应具体的分布

abline(h=0)
polygon(x2,y2,col="red")

#加上标题
title("Tail Probability for t(19)")
text(c(-4.1,-2.5),c(0.02,-0.007),c("p-value=0.005","t=-2.8609"))

#标准正态分布的分位数

qnorm(0.005,0,1)#计算分位数

x<-seq(-6,6,length=1000)
y<-dnorm(x,0,1)
plot(x,y,type="l",ylab="density of N(0,1)",xlim=c(-5,5),ylim=c(0,0.5))

r1<--6
r2<--2.575829

x2<-c(r1,r1,x[x<r2&x>r1],r2,r2)                #选择区域
y2<-c(0,dnorm(c(r1,x[x<r2&x>r1],r2),0,1),0)    #要对应具体的分布

abline(h=0)
polygon(x2,y2,col="red")

title("Tail Probability for N(0,1)")
text(c(-4.1,-2.5),c(0.02,-0.007),c("p-value=0.005","t=-2.8609"))

#卡方(5)的分位数

qchisq(0.025,5)#计算分位数

x<-seq(0,10,length=1000)
y<-dchisq(x,5)
plot(x,y,type="l",ylab="density of X^2(5)")

r1<-0
r2<-0.8312
x2<-c(r1,r1,x[x<r2&x>r1],r2,r2)                 #选择区域
y2<-c(0,dchisq(c(r1,x[x<r2&x>r1],r2),5),0)      #对应具体的分布

abline(h=0)
polygon(x2,y2,col="red")

title("Tail Probability for X^2(5)")
text(c(0.4,0.5),c(0.02,-0.007),c("p-value=0.005","t=0.8312"))

#F(2,2)的分位数

qf(0.9,2,2)              #计算分位数

x<-seq(0,15,length=1000)
y<-df(x,2,2)
plot(x,y,type="l",ylab="density of F(2,2)")

r1<-0
r2<-9
x2<-c(r1,r1,x[x<r2&x>r1],r2,r2)                 #选择区域
y2<-c(0,df(c(r1,x[x<r2&x>r1],r2),2,2),0)        #对应具体的分布

abline(h=0)
polygon(x2,y2,col="red")

title("Tail Probability for F(2,2)")
text(c(7.8,9.1),c(0.05,0.1),c("p-value=0.9","t=9"))










k线图

#采用默认的ChartSeries绘制K线图
library(quantmod)
data_origin=read.csv("data.csv",header = F)
data<-data.frame(Open=data_origin[,2],High=data_origin[,3],Low=data_origin[,4],Close=data_origin[,5],Volume=data_origin[,7],Adjusted=data_origin[,6])
rownames(data)<-as.Date(as.character(data_origin$V1),"%Y%m%d")
chartSeries(data)




PS:获取当前工作目录和设置工作目录的方法

getwd()

setwd(‘E:\R工作目录’)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: