科研代码大全|小云带学R语言之代码实战篇

小云给大家介绍一个单细胞分析的小工具:单细胞分析(http://www.biocloudservice.com/366/366.php),能够在线输入数据,并快速输出结果,大家一起来使用吧。欢迎来和小云交流讨论哦。
大家好,本期小云将通过一道R语言的实战题目带大家熟悉R语言的内容,跟着小云一起来学习吧!
1. 有一个外部文件为class.txt,存储的是某个班级学生的姓名、年龄、身 高、体重和性别的信息。
1)此文件读入到R中并把它记为数据框class。
> class<-read.table(choose.files())
2)给数据框class的列命名为name,age,height,weight,sex
> names(class)<-c("name","age","height","weight","sex")

3)添加一个根据年龄分组的变量rank,分成两组: low(小于等于14岁) high(大于等于15岁)
> rank<-class$age
> class<-cbind(class,rank)
> class$rank[rank<=14]<-"low"
> class$rank[rank>=15]<-"high"

4)按性别画出体重的箱型图(水平,凹槽,颜色控制等)
>boxplot(weight~sex,class$sex,class$weight,notch=T,col=c("red","blue"),names=c("男","女"),horizontal=T)

5)根据性别及年龄分组画出箱型图
>boxplot(height~sex,data=class,notch=F,col=c("red"),names=c("男","女"),boxwex=0.25,at=1:2-0.2)
>boxplot(height~sex,data=class,add=T,at=1:2+0.2,subset=rank=="low",col=c("green"),boxwex=0.25)
> legend("bottom",c("high","low"),fill=c("red","green"))

6)画出体重与身高关系的散点图
> plot(weight,height,col="blue")

7)通过设置参数mfrow将所有图放到同一页中并保存为一个png文件
> par(mfrow=c(1,3))
>boxplot(weight~sex,class$sex,class$weight,notch=T,col=c("red","blue"),names=c("男","女"),horizontal=T)
>boxplot(height~sex,data=class,notch=F,col=c("red"),names=c("男","女"),boxwex=0.25,at=1:2-0.2)
>boxplot(height~sex,data=class,add=T,at=1:2+0.2,subset=rank=="low",col=c("green"),boxwex=0.25)
> legend("bottom",c("high","low"),fill=c("red","green"))
> plot(weight,height,col="blue")
> png("boxplot.png")
> dev.off
2.
> a<-t(VADeaths)
> barplot(a,beside = T,col = c("red","blue","green","purple"),legend=rownames(a))

以上就是本期的内容啦,跟着小云每天进步一点点吧!
请持续关注小云,我们下期见~
筛选免疫基因对小工具:www.biocloudservice.com/589/589.php
