几个R语言画图小程序分享
2017-08-16 14:02
281 查看
三维图
等高线图
区间概率变化
大物实验报告数据分析
饼图
两侧分位数
分位数图
k线图
PS:获取当前工作目录和设置工作目录的方法
getwd()
setwd(‘E:\R工作目录’)
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工作目录’)
相关文章推荐
- 几个画图小程序的分享(隐含波动率)
- 分享几个Android开发有用的程序代码
- 【分享】几个高仿安卓程序,大家可以看看
- 分享几个Android开发有用的程序代码
- [置顶] 分享几个微信小程序的视频教程
- 分享几个Android开发有用的程序代码
- Python实战小程序利用matplotlib模块画图代码分享
- 分享几个java小程序代码
- 分享几个Android开发有用的程序代码
- 分享几个经典C程序
- 微信新版 分享 登录 分享小程序 遇到 的问题
- java软件安装的几个小技巧分享
- 分享几个android开源项目
- 分享几个Linux下的码流分析工具
- [分享]程序大战-学习资料
- 一个自己编写的象棋程序,可实现走棋谱(如炮二平五),工程完成度%96,跟大家分享一下,下一步要做残局,欢迎交流
- 模拟mspaint画图程序
- 1,2,5,34,12,4,12,7 这几个数字用冒泡排序法的程序
- 工作中的几个程序例子
- 学51单片机中的几个小程序