R绘图

来源:互联网 发布:社会融资规模数据 编辑:程序博客网 时间:2024/04/29 16:07

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
0 0