R绘图

mac 上安装R: http://cran.r-project.org/bin/macosx/


R绘图常用的两个包

ggplot2:http://ggplot2.org/

lattice:http://cran.r-project.org/web/packages/lattice/index.html


推荐使用文艺版--ggplot2

安装

> install.packages("ggplot2")

练习使用,可参考3​0​分​钟​学​会​g​g​p​l​o​t​2

查看包ggplot2中的数据集

> data(package='ggplot2')

查看其中diamonds数据集的信息

> str(diamonds)

> summary(diamonds)
> attributes(diamonds)
以及
> class(diamonds)
[1] "data.frame"
> mode(diamonds)
[1] "list"

> library(ggplot2)
> p<-ggplot(mpg,aes(x=cty,y=hwy))


遇到问题:

中文乱码:(难过


console
通过theme()来控制字体
p+geom_point(aes(colour=class,size=displ),alpha=0.5,position="jitter")+stat_smooth()+scale_size_continuous(range=c(4,10))+facet_wrap(~year,ncol=1)+opts(title='汽车油耗与型号')+labs(y='每加仑高速公路行驶距离',x='每加仑城市公路行驶距离')+guides(size=guide_legend(title='排量'),colour=guide_legend(title='车型'),override.aes=list(size=5))+theme(plot.title=element_text(family="STKaiti",size=14),axis.title=element_text(family="STKaiti",size=14),legend.title=element_text(family="STKaiti",size=14))



dev
以输出到pdf为例:
> pdf(family="GB1",file='a.pdf')
>p+geom_point(aes(colour=class,size=displ),alpha=0.5,position="jitter")+stat_smooth()+scale_size_continuous(range=c(4,10))+facet_wrap(~year,ncol=1)+opts(title='汽车油耗与型号')+labs(y='每加仑高速公路行驶距离',x='每加仑城市公路行驶距离')+guides(size=guide_legend(title='排量'),colour=guide_legend(title='车型'),override.aes=list(size=5))
> dev.off()

效果


附上一份google的R规范
https://google-styleguide.googlecode.com/svn/trunk/Rguide.xml
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值