第5课更复杂技巧画多幅图输出到文件函数图象向量场动态气泡和动画_第1页
第5课更复杂技巧画多幅图输出到文件函数图象向量场动态气泡和动画_第2页
第5课更复杂技巧画多幅图输出到文件函数图象向量场动态气泡和动画_第3页
第5课更复杂技巧画多幅图输出到文件函数图象向量场动态气泡和动画_第4页
第5课更复杂技巧画多幅图输出到文件函数图象向量场动态气泡和动画_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

法律【】和幻灯片为炼数成金网络课程的教学资料,所有资料只能在课程内使用,不得在课程以外范围散播,违者将可能被法律和经济责任。课程详情

炼数成金培训http:

2013.2.82DATAGURU专业数据分析ggthemes包2013.2.83DATAGURU专业数据分析例子ear)))+传统ggplot2风格ggplot(mtcars,aes(x=mpg,y=wt,size=cyl,colour=factgeom_point(alpha=.5)+

#scale_size_area()+#区域和数值成比例

scale_colour_brewer(palette="Set1")+ggtitle("Motor

Trend

Car

Road

Tests")2013.2.84DATAGURU专业数据分析例子ear)))+Excel风格ggplot(mtcars,aes(x=mpg,y=wt,size=cyl,colour=factgeom_point()+theme_excel()

+scale_fill_excel()+ggtitle("Motor

Trend

Car

Road

Tests")2013.2.85DATAGURU专业数据分析例子ear)))+Economist风格ggplot(mtcars,aes(x=mpg,y=wt,size=cyl,colour=factgeom_point(alpha=.5)+theme_economist()

+scale_colour_economist()+ggtitle("Motor

Trend

Car

Road

Tests")2013.2.86DATAGURU专业数据分析例子ear)))+深色风格ggplot(mtcars,aes(x=mpg,y=wt,size=cyl,colour=factgeom_point()+theme_solarized(light=FALSE)

+scale_colour_solarized("red")+ggtitle("Motor

Trend

Car

Road

Tests")2013.2.87DATAGURU专业数据分析输出到文件ggsave()qplot(mpg,

wt,

data

=

mtcars)ggsave(file

=

"output.pdf")pdf(file

=

"output.pdf",

width

=

6,

height=

6)#

If

inside

a

script,

you

will

need

to

explicitly

print()

plotsqplot(mpg,

wt,

data

=

mtcars)qplot(wt,

mpg,

data

=

mtcars)dev.off()2013.2.88DATAGURU专业数据分析结果2013.2.89DATAGURU专业数据分析在同一页面上画多幅图先产生三幅图(a

<-

qplot(date,

unemploy,

data

=

economics,

geom

=

"line"))(b

<-

qplot(uempmed,

unemploy,

data

=

economics)

+geom_smooth(se

=

F))(c

<-

qplot(uempmed,

unemploy,

data

=

economics,

geom="path"))2013.2.810DATAGURU专业数据分析viewport()Library(grid)#

A

viewport

that

takes

up

the

entire

plot

devicevp1

<-

viewport(width

=

1,

height

=

1,

x

=

0.5,

y

=

0.5)vp1

<-

viewport()#

A

viewport

that

takes

up

half

the

width

and

half

theheight,#

located

in

the

middle

of

the

plot.vp2

<-

viewport(width

=

0.5,

height

=

0.5,

x

=

0.5,

y

=

0.5)vp2

<-

viewport(width=

0.5,

height

=

0.5)#

A

viewport

that

is

2cm

x

3cm

located

in

the

centervp3

<-

viewport(width

=

unit(2,

"cm"),

height

=

unit(3,

"cm"))2013.2.811DATAGURU专业数据分析实验pdf("polishing-subplot-1.pdf",

width

=

4,

height

=

4)subvp

<-

viewport(width

=

0.4,

height

=

0.4,

x

=

0.75,

y

=

0.35)bprint(c,

vp

=subvp)dev.off()2013.2.812DATAGURU专业数据分析改进csmall

<-

c

+theme_gray(9)

+labs(x

=

NULL,

y

=

NULL)

+theme(plot.margin

=

unit(rep(0,

4),

"lines"))pdf("polishing-subplot-2.pdf",

width

=

4,

height

=

4)bprint(csmall,

vp

=

subvp)dev.off()2013.2.813DATAGURU专业数据分析结果2013.2.814DATAGURU专业数据分析指定比例pdf("polishing-layout.pdf",

width

=

8,

height

=6)grid.ne

ge()pushViewport(viewport(layout

=

grid.layout(2,

2)))vplayout

<-

function(x,

y)viewport(layout.pos.row

=

x,layout.pos.col

=

y)print(a,

vp

=

vplayout(1,

1:2))print(b,

vp

=

vplayout(2,

1))print(c,

vp

=

vplayout(2,

2))dev.off()2013.2.815DATAGURU专业数据分析结果2013.2.816DATAGURU专业数据分析画函数图像p

<-

ggplot(data.frame(x=c(-3,3)),

aes(x=x))p

+

stat_function(fun=dnorm)2013.2.817DATAGURU专业数据分析画函数图像myfun

<-

function(xvar)

{1/(1

+

exp(-xvar

+

10))}ggplot(data.frame(x=c(0,

20)),

aes(x=x))

+

stat_function(fun=myfun)2013.2.818DATAGURU专业数据分析画微积分学中常见的曲边梯形#

Return

dnorm(x)

for

0

<

x

<

2,

and

NA

for

all

other

xdnorm_limit

<-

function(x)

{y

<-

dnorm(x)y[x<

0

|

x

>

2]

<-NAreturn(y)}#

ggplot()

with

dummy

datap

<-

ggplot(data.frame(x=c(-3,

3)),

aes(x=x))p

+stat_function(fun=dnorm_limit,

geom="area",

fill="blue",

alpha=0.2)+stat_function(fun=dnorm)2013.2.819DATAGURU专业数据分析结果2013.2.820DATAGURU专业数据分析ggplot2画向量场2013.2.821DATAGURU专业数据分析向量场2013.2.822DATAGURU专业数据分析向量场2013.2.823DATAGURU专业数据分析让图形动起来并非ggplot2包的功能library(rgl)plot3d(mtcars$wt,

mtcars$disp,

mtcars$mpg,

type="s",

size=0.75,

lit=FALSE)play3d(spin3d())2013.2.824DATAGURU专业数据分析在R中实现动态气泡图/blob/gh-使用ggplot2包,animation包,ffmpeg数据

/mbostock/bost.ocks.

pages/mike/nations/nations.json代码

:2013.2.825DATAGURU专业数据分析代码#Import

the

Strange

Datasetrawdata=readLines("nations.json")#Get

the

useful

wordsdata=unlist(strsplit(rawdata,"\\[\\{\"|\":\"|\",\"|\":\\[\\[|\\],\\[|\\]\\],\"|\\]\\]\\},\\{\"|\\]\\]\\}\\]"))data=data[which(data!="")]n=length(data)2013.2.826DATAGURU专业数据分析代码#Set

indexnameind=which(data=="name")regind=which(data=="region")incind=which(data=="

e")popind=which(data=="population")lifind=which(data=="lifeExpectancy")endpoint=nameind-1endpoint=endpoint[2:180]endpoint[180]=45999name=data[nameind+1]region=data[regind+1]2013.2.827DATAGURU专业数据分析代码#An

empty

data.frametypeinitdfr=data.frame(name=rep(0,210),region=rep(0,210),yee=rep(0,210),pop=rep(0,210),life=rep(0,210))#An

string

split

function

only

working

for

thecommasplt=function(x)

return(as.numeric(unlist(strsplit(x,","))))#Linear

interpolation

function,

for

those

zero

dataitpl=function(a){ind=which(a>0)if

(ind[1]>1)a[1:(ind[1]-1)]=rep(a[ind[1]],ind[1]-1)n=length(ind)if

(ind[n]<length(a))a[(ind[n]+1):length(a)]=rep(a[ind[n]],length(a)-ind[n])for

(i

in

1:(n-1))a[ind[i]:ind[i+1]]=rep(a[ind[i]],ind[i+1]-ind[i]+1)+(seq(ind[i],ind[i+1],1)-ind[i])*(a[ind[i+1]]-a[ind[i]])/(ind[i+1]-ind[i])return(a)}2013.2.828DATAGURU专业数据分析代码#Drag

data

information

from

wordstbls=NULLfor

(i

in

1:180){dfr=initdfrdfr$name=rep(name[i],210)dfr$region=rep(region[i],210)for

(

j

in

(incind[i]+1):(popind[i]-1)){tmp=sp

ata[

j])ii=which(dfr$year==tmp[1])dfr$

e[ii]=tmp[2]}2013.2.829DATAGURU专业数据分析代码for

(

j

in

(popind[i]+1):(lifind[i]-1)){tmp=sp

ata[

j])ii=which(dfr$year==tmp[1])dfr$pop[ii]=tmp[2]}for

(

j

in(lifind[i]+1):endpoint[i]){tmp=sp

ata[

j])ii=which(dfr$year==tmp[1])dfr$life[ii]=tmp[2]}tbls=rbind(tbls,dfr)}2013.2.830DATAGURU专业数据分析代码#Two

country

with

only

one

record,

meaninglessind=which(tbls$name=="Mayotte")tbls=tbls[-ind,]ind=which(tbls$name=="Tokelau")tbls=tbls[-ind,]name=name[c(-28,-177)]region=region[c(-28,-177)]2013.2.831DATAGURU专业数据分析代码#Linear

interpolationfor

(i

in

1:178)e[ind]){ind=(210*(i-1)+1):(i*210)tbls$

e[ind]=itpl(tbls$tbls$pop[ind]=itpl(tbls$pop[ind])tbls$life[ind]=itpl(tbls$life[ind])}2013.2.832DATAGURU专业数据分析代码require(ggplot2)#Draw

function

with

ggplot2drawit=function(yr,scl=15){ind=which(tbls$year==yr)d.f=data.frame(yr=yr)p=ggplot(aes(x=log(e),y=life,size=pop,colour=as.factor(region)),pch=21,data=tbls[ind,])p+geom_point(show_guide=

FALSE)+geom_point(sh =

1,colour

=

"black",show_guide

=

FALSE)+xlim(5.5,11.7)+ylim(10,83)+scale_area(range

=

c(1,

scl))+annotate("text",

x=10,

y=15,

label

=yr,size=30,color="grey")}#drawit(1800)2013.2.833DATAGURU专业数据分析代码#Automatically

repeat

the

drawing

procedurefinaldraw=function(a,b){for

(i

in

1:10)print(drawit(a))for

(i

in

a:b)print(drawit(i))for

(i

in

1:10)print(drawit(b))}2013.2.834DATAGURU专业数据分析代码#finaldraw(1800,2009)requ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论