学习小组-day4-Mingwei Guo:R学习
2020-04-20 本文已影响0人
吹不散的烽烟
1.R语言安装
官网下载windows版本
2.Rstudio安装
官网下载windows版本
3.外观设置
![](https://img.haomeiwen.com/i13200130/427e4aef9f4f999f.png)
![](https://img.haomeiwen.com/i13200130/3ec5aec337657b14.png)
4.学习作图:
1.散点图
plot(rnorm(50))
![](https://img.haomeiwen.com/i13200130/54a42e53a1099375.png)
2.箱线图
boxplot(iris$Sepal.Length~iris$Species,col = c("lightblue","lightyellow","lightpink"))
![](https://img.haomeiwen.com/i13200130/2e092550ec9152ab.png)
3.热图
安装加载pheatmap
install.packages("pheatmap")
library(pheatmap)
创建一个矩阵,设置行名为Gene1-20和列名Test1-10
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
![](https://img.haomeiwen.com/i13200130/ecd4edefbc3579f0.png)
画图
pheatmap(test)
![](https://img.haomeiwen.com/i13200130/be689b6439d0b526.png)
设置kmean数值
pheatmap(test, kmeans_k = 2)
![](https://img.haomeiwen.com/i13200130/cbf9a60903cb93c9.png)
进行归一化
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
![](https://img.haomeiwen.com/i13200130/6b11c8e66a525576.png)
修改颜色
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
![](https://img.haomeiwen.com/i13200130/cc71ac33a0ad5639.png)
修改行或者列聚类
pheatmap(test, cluster_row = FALSE)
![](https://img.haomeiwen.com/i13200130/4d974d0cc6a0170c.png)
设置图像长宽
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
![](https://img.haomeiwen.com/i13200130/5ffe0ea93d5ffb94.png)
添加注释
annotation_col = data.frame( CellType = factor(rep(c("CT1", "CT2"), 5)), Time = 1:5)
rownames(annotation_col) = paste("Test", 1:10, sep = "")
annotation_row = data.frame( GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))))
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)
![](https://img.haomeiwen.com/i13200130/7aa2df2343adb92a.png)
修改行文本角度
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")
![](https://img.haomeiwen.com/i13200130/746e553a5771c0a8.png)
分裂行或列
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14), cutree_col = 2)
![](https://img.haomeiwen.com/i13200130/e59512ff042a5ca3.png)
添加想要的行名
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "","", "", "Il10", "Il15", "Il1b")
pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
![](https://img.haomeiwen.com/i13200130/f4c16782f667b6fb.png)
5.基本操作:
R project 管理工作目录
getwd() #查看工作目录
setwd() #设置工作目录
新建一个R project
![](https://img.haomeiwen.com/i13200130/4d2bd0edb2986391.png)
![](https://img.haomeiwen.com/i13200130/bd8e2ce022f80e00.png)
显示文件列表
> dir()
[1] "Test.Rproj"
>list.files()
[1] "Test.Rproj"
简单运算
> 1+5
[1] 6
赋值
> a<-666
> a
[1] 666
删除变量
rm(list=ls()) #清空所有变量
历史命令
history()
![](https://img.haomeiwen.com/i13200130/5ff03ef337bdcb20.png)
清空控制台
Ctrl+l