R语言入门班
2016-03-19 09:06
274 查看
感谢: 吴明昊老师 Swirl包作者 《Elegant Graphics for Dataanlysis》书作者 《R Graphics Cookbook》书作者
第一课 R初体验(12 h)
1、安装R
2、安装RStudio
3.在RStudio下输入以下命令:
install.packages("swirl")
安装Swirl
4、调用Swirl:
library(swirl)swirl()install_from_swirl("R Programming")
然后就可以开始使用Swirl了。
theme_set(theme_bw())
R工作目录
file.create("mytest.R")
list.files()
unlink('testdir2',recursive= TRUE)
加载数据
R中数组
c(1,1,2,4,5,7)
attach(mpg)
save(baochunsm,"wenjianming")
ggsave("*.png",width =5,height=5)
R中向量
paste(my_char,collapse =" ")
plotmath
R中缺失值处理
NA
NaN
To generate NaN,try dividing (using a forward slash) 0 by 0 .
R构造子集向量
矩阵和数据结构
my_matrix2<- matrix(1:20,4,5)
cbind(patients,my_matrix)
colnames(my_data) <- cnames
R中逻辑表达式
all
any
R中功能
R中申请和供给
lapply
sapply
vapply and tapply
flips <- sample(c(0,1), 100, replace = TRUE, prob = c(0.3, 0.7))
模拟
仿真
3D图包 (latticeExtra)
第二课 R基本语法
youtube学习
ggplot2:Elegant Graphics for Dataanlysis http://www.cookbook-r.com/Graphs/ http://www.datavizcatalogue.com
#Referrence Book
#ggplot2: Elegant Graphics for Data Analysis (Use R!):
#http://www.amazon.com/ggplot2-Elegant-Graphics-Data-Analysis/dp/0387981403
#R Graphics Cookbook:
#http://www.cookbook-r.com/Graphs/
#http://www.amazon.com/R-Graphics-Cookbook-Winston-Chang/dp/1449316956
#install.packages("ggplot2")
library(ggplot2)
#Scatter Plot
plot(mtcars$wt, mtcars$mpg)
#Line Graph
plot(pressure$temperature, pressure$pressure, type="l")
points(pressure$temperature, pressure$pressure) #add points
lines(pressure$temperature, pressure$pressure/2, type="l",col="red")
points(pressure$temperature, pressure$pressure/2,col="red")
#Bar Graph
barplot(table(mtcars$cyl))
#Histogram
hist(mtcars$mpg, breaks=10)
#Box Plot
boxplot(mtcars$mpg)
#Plotting a Function Curve
curve(x^3 - 5*x, from=-4, to=4)
myfun <- function(xvar) {
1/(1 + exp(-xvar + 10))
}
curve(myfun(x), from=0, to=20)
#Basic use
dat=diamonds
qplot(carat, price, data = diamonds)
qplot(log(carat), log(price), data = diamonds)
qplot(carat, x * y * z, data = diamonds)
#Colour, size, shape and other aesthetic attributes
dsmall = diamonds[sample(nrow(diamonds), 100), ]
qplot(carat, price, data = dsmall, colour = color)
qplot(carat, price, data = dsmall, shape = cut)
#make a semi-transparent colour, from 0 to 1
qplot(carat, price, data = diamonds, alpha = I(1/10))
qplot(carat, price, data = diamonds, alpha = I(1/100))
qplot(carat, price, data = diamonds, alpha = I(1/200))
#Plot geoms
#geom = "point" draws points to produce a scatterplot. This is the default
#when you supply both x and y arguments to qplot().
#geom = "smooth" fits a smoother to the data and displays the smooth and
#its standard error.
#geom = "boxplot" produces a box-and-whisker plot to summarise the
#distribution of a set of points.
#geom = "path" and geom = "line" draw lines between the data points.
#Traditionally these are used to explore relationships between time and
#another variable, but lines may be used to join observations connected in
#some other way.
#geom = "histogram"
#geom = "freqpoly"
#geom = "density"
#geom = "bar"
qplot(carat, price, data = dsmall, geom = c("point", "smooth"))
library(splines)
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "lm")
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "lm", formula = y ~ ns(x,5))
#color, fill
qplot(color, price, data = dsmall, geom = "boxplot")
qplot(color, price, data = diamonds, geom = "boxplot",fill=I("blue"))
qplot(color, price, data = dsmall, geom = "boxplot",size=2)
#geom_boxplot()
qplot(color, price,
data = dsmall,
geom = "boxplot") + geom_boxplot(outlier.colour = "green",
outlier.size = 10,
fill = "red",
colour = "blue",
size=2)
qplot(carat, data = diamonds, geom = "histogram",colour=color,fill=color)
qplot(carat, data = diamonds, geom = "density")
qplot(carat, data = diamonds, geom = "density", color = color)
qplot(carat, data = diamonds, geom = "density", fill = color)
qplot(color, data = diamonds, geom = "bar",fill=color)
qplot(date, unemploy / pop, data = economics, geom = "line")
qplot(date, uempmed, data = economics, geom = "line")
qplot(unemploy / pop, uempmed, data = economics, geom = c("point", "path"))
year = function(x) as.POSIXlt(x)$year + 1900
qplot(unemploy / pop, uempmed, data = economics,
geom = "path", colour = year(date))
#Lecture 2
#minghaowu_2015@163.com
setwd("~/documents/R programming/ggplot2")
library(ggplot2)
attach(mpg)
head(mpg)
#Review
qplot(displ, hwy, data = mpg, colour = factor(cyl))
qplot(displ, hwy, data = mpg,
colour = factor(cyl),
geom=c("smooth","point"),
method="lm")
qplot(displ, hwy, data=mpg, facets = . ~ year) + geom_smooth()
#Save
p = qplot(displ, hwy, data = mpg, colour = factor(cyl))
summary(p)
save(p, file = "plot.rdata")
load("plot.rdata")
ggsave("plot.png", width = 5, height = 5)
#Build a plot layer by layer
p = ggplot(diamonds, aes(carat, price, colour = cut))
p
p = p + layer(geom = "point")
#ggplot(diamonds, aes(carat, price, colour = cut)) + layer(geom = "point")
p
#layer(geom, geom_params, stat, stat_params, data, mapping, position)
p <- ggplot(diamonds, aes(x = carat))
p <- p + layer(
geom = "bar",
geom_params = list(fill = "steelblue"),
stat = "bin",
stat_params = list(binwidth = 0.5)
)
p
#basic form
#geom_XXX(mapping, data, ..., geom, position)
#stat_XXX(mapping, data, ..., stat, position)
#Example 1
ggplot(msleep, aes(sleep_rem / sleep_total, awake)) + geom_point()
#is equivalent to
qplot(sleep_rem / sleep_total, awake, data = msleep)
#Example 2
qplot(sleep_rem / sleep_total,
awake, data = msleep,
geom = c("point", "smooth"))
#is equivalent to
ggplot(msleep, aes(sleep_rem / sleep_total, awake)) + geom_point() + geom_smooth()
#is equivalent to
plot = ggplot(msleep, aes(sleep_rem / sleep_total, awake))
plot = plot + geom_point() + geom_smooth()
plot
#Example 3 (cont.)
bestfit = geom_smooth(method = "lm",
se = T,
colour = "steelblue",
alpha=0.5,
size = 2)
qplot(sleep_rem, sleep_total, data = msleep) + bestfit
qplot(displ, hwy, data=mpg, facets = . ~ year) + bestfit
#Transform the data in the plot using %+%
p = ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point(size=5)
p
mtcars = transform(mtcars, mpg = mpg ^ 2)
p %+% mtcars
#Aesthetic mappings
qplot(color, price, data = diamonds, geom = "boxplot",fill=I("blue"))
#Aesthetic mappings
library(ggplot2)
#Plots and Layers
p = ggplot(mtcars, aes(x = mpg, y = wt))
p + geom_point()
p + geom_point(aes(colour = factor(cyl)))
p + geom_point(aes(y = disp))
#Add aes(colour = cyl) ======> aes(mpg, wt, colour = cyl)
#Override aes(y = disp) ======> aes(mpg, disp)
#Remove aes(y = NULL) ======> aes(mpg)
#Setting vs. mapping
# Mapping: aes(colour = cut))
# Setting: color = "red"
p + geom_point(colour = "green")
# is different from
p + geom_point(aes(colour = "blue"))
#When "green" is mapped to colour, it is treated
#as a regular value and scaled with the default colour scale.
#Grouping
library(nlme)
data(package="nlme")
?Oxboys
head(Oxboys)
str(Oxboys)
p = ggplot(Oxboys, aes(age, height, group = Subject)) + geom_line()
ggplot(Oxboys, aes(age, height, group = 1)) + geom_line()
#Different groups on different layers
p + geom_smooth(aes(group = Subject), method="lm", se = F)
p + geom_smooth(aes(group = 1), method="lm", se = F, size=2)
#Overriding the default grouping.
#no need to specify the group aesthetic here; the default grouping
#works because occasion is a discrete variable
boysbox = ggplot(Oxboys, aes(Occasion, height)) + geom_boxplot()
boysbox
boysbox + geom_line(aes(group = Subject), colour = "blue")
#Geom
#Stat
#Position adjustments
#dodge: Adjust position by dodging overlaps to the side
#fill: Stack overlapping objects and standardise have equal height
#identity: Do not adjust position
#jitter: Jitter points to avoid overplotting
#stack: Stack overlapping objects on top of one another
ggplot(diamonds,
aes(clarity, fill = cut)) + geom_bar(position = "stack")
ggplot(diamonds,
aes(clarity, fill = cut)) + geom_bar(position = "fill")
ggplot(diamonds,
aes(clarity, fill = cut)) + geom_bar(position = "dodge")
第三课 qplot作图
#
plot(mtcars$wt,mtcars$mpg,type ="l")
#
hist(mtcars$mpg,breaks =10)
#
boxplot(mtcars$mpg,pars = list(boxwex = 0.4, staplewex = 0.9, outwex = 0.5))
#plotting a Function Curve
curve(x^3-5*x,form =-4,to =4)
myfun <- function(xvar){
1/(1+exp(-xvar +10))
}
curve(myfun(x),form =0,to=20)
#
qplot(carat,price,data =diamonds,shape=cut,colour=color)
#geom 增添一些功能
setwed("workplace")
第四课 ggplot作图
ggplot相对于qplot的优势在于aes(添加属性)、layer等修饰操作,geom和stat基本上是知道与不知道的问题。同一张图代码写法可能有多重,You are limited by your imagination.
#柱状图
qplot(color,data =diamonds,geom="bar",fill=color)
#线型图
qplot(carat,data =diamonds,geom="histogram",colour=color)
#热图
清洗数据存入csv
str()
rownames()
colnames()
library(gplots)
getwd()
x <-read.csv("NodGenes.csv”,check.name =FALSE)
y <-data.matrix(NodGenes)
heatmap.2(y,main ="Sample",trace ="none",margins= c(10,12),cexRow =0.5,Rowv= FALSE,Colv=FALSW,Key =FALSE)
yb <-colorRampPalette(c("yellow","white","darkblue"))
heatmap.2(y,col =yb, main ="Sample",trace ="none",margins= c(10,12),cexRow =0.5,Rowv= FALSE,Colv=FALSW,Key)
第五课 其他生物信息相关图
#相关图
#地图
#饼图
#聚类图
#曼哈顿图
#基因分布图
#群体结构分布图
#基因组共线性图
#LD图
#Circos图
第一课 R初体验(12 h)
1、安装R
2、安装RStudio
3.在RStudio下输入以下命令:
install.packages("swirl")
安装Swirl
4、调用Swirl:
library(swirl)swirl()install_from_swirl("R Programming")
然后就可以开始使用Swirl了。
theme_set(theme_bw())
R工作目录
file.create("mytest.R")
list.files()
unlink('testdir2',recursive= TRUE)
加载数据
R中数组
c(1,1,2,4,5,7)
attach(mpg)
save(baochunsm,"wenjianming")
ggsave("*.png",width =5,height=5)
R中向量
paste(my_char,collapse =" ")
plotmath
R中缺失值处理
NA
NaN
To generate NaN,try dividing (using a forward slash) 0 by 0 .
R构造子集向量
矩阵和数据结构
my_matrix2<- matrix(1:20,4,5)
cbind(patients,my_matrix)
colnames(my_data) <- cnames
R中逻辑表达式
all
any
R中功能
R中申请和供给
lapply
sapply
vapply and tapply
flips <- sample(c(0,1), 100, replace = TRUE, prob = c(0.3, 0.7))
模拟
仿真
3D图包 (latticeExtra)
第二课 R基本语法
youtube学习
ggplot2:Elegant Graphics for Dataanlysis http://www.cookbook-r.com/Graphs/ http://www.datavizcatalogue.com
#Referrence Book
#ggplot2: Elegant Graphics for Data Analysis (Use R!):
#http://www.amazon.com/ggplot2-Elegant-Graphics-Data-Analysis/dp/0387981403
#R Graphics Cookbook:
#http://www.cookbook-r.com/Graphs/
#http://www.amazon.com/R-Graphics-Cookbook-Winston-Chang/dp/1449316956
#install.packages("ggplot2")
library(ggplot2)
#Scatter Plot
plot(mtcars$wt, mtcars$mpg)
#Line Graph
plot(pressure$temperature, pressure$pressure, type="l")
points(pressure$temperature, pressure$pressure) #add points
lines(pressure$temperature, pressure$pressure/2, type="l",col="red")
points(pressure$temperature, pressure$pressure/2,col="red")
#Bar Graph
barplot(table(mtcars$cyl))
#Histogram
hist(mtcars$mpg, breaks=10)
#Box Plot
boxplot(mtcars$mpg)
#Plotting a Function Curve
curve(x^3 - 5*x, from=-4, to=4)
myfun <- function(xvar) {
1/(1 + exp(-xvar + 10))
}
curve(myfun(x), from=0, to=20)
#Basic use
dat=diamonds
qplot(carat, price, data = diamonds)
qplot(log(carat), log(price), data = diamonds)
qplot(carat, x * y * z, data = diamonds)
#Colour, size, shape and other aesthetic attributes
dsmall = diamonds[sample(nrow(diamonds), 100), ]
qplot(carat, price, data = dsmall, colour = color)
qplot(carat, price, data = dsmall, shape = cut)
#make a semi-transparent colour, from 0 to 1
qplot(carat, price, data = diamonds, alpha = I(1/10))
qplot(carat, price, data = diamonds, alpha = I(1/100))
qplot(carat, price, data = diamonds, alpha = I(1/200))
#Plot geoms
#geom = "point" draws points to produce a scatterplot. This is the default
#when you supply both x and y arguments to qplot().
#geom = "smooth" fits a smoother to the data and displays the smooth and
#its standard error.
#geom = "boxplot" produces a box-and-whisker plot to summarise the
#distribution of a set of points.
#geom = "path" and geom = "line" draw lines between the data points.
#Traditionally these are used to explore relationships between time and
#another variable, but lines may be used to join observations connected in
#some other way.
#geom = "histogram"
#geom = "freqpoly"
#geom = "density"
#geom = "bar"
qplot(carat, price, data = dsmall, geom = c("point", "smooth"))
library(splines)
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "lm")
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "lm", formula = y ~ ns(x,5))
#color, fill
qplot(color, price, data = dsmall, geom = "boxplot")
qplot(color, price, data = diamonds, geom = "boxplot",fill=I("blue"))
qplot(color, price, data = dsmall, geom = "boxplot",size=2)
#geom_boxplot()
qplot(color, price,
data = dsmall,
geom = "boxplot") + geom_boxplot(outlier.colour = "green",
outlier.size = 10,
fill = "red",
colour = "blue",
size=2)
qplot(carat, data = diamonds, geom = "histogram",colour=color,fill=color)
qplot(carat, data = diamonds, geom = "density")
qplot(carat, data = diamonds, geom = "density", color = color)
qplot(carat, data = diamonds, geom = "density", fill = color)
qplot(color, data = diamonds, geom = "bar",fill=color)
qplot(date, unemploy / pop, data = economics, geom = "line")
qplot(date, uempmed, data = economics, geom = "line")
qplot(unemploy / pop, uempmed, data = economics, geom = c("point", "path"))
year = function(x) as.POSIXlt(x)$year + 1900
qplot(unemploy / pop, uempmed, data = economics,
geom = "path", colour = year(date))
#Lecture 2
#minghaowu_2015@163.com
setwd("~/documents/R programming/ggplot2")
library(ggplot2)
attach(mpg)
head(mpg)
#Review
qplot(displ, hwy, data = mpg, colour = factor(cyl))
qplot(displ, hwy, data = mpg,
colour = factor(cyl),
geom=c("smooth","point"),
method="lm")
qplot(displ, hwy, data=mpg, facets = . ~ year) + geom_smooth()
#Save
p = qplot(displ, hwy, data = mpg, colour = factor(cyl))
summary(p)
save(p, file = "plot.rdata")
load("plot.rdata")
ggsave("plot.png", width = 5, height = 5)
#Build a plot layer by layer
p = ggplot(diamonds, aes(carat, price, colour = cut))
p
p = p + layer(geom = "point")
#ggplot(diamonds, aes(carat, price, colour = cut)) + layer(geom = "point")
p
#layer(geom, geom_params, stat, stat_params, data, mapping, position)
p <- ggplot(diamonds, aes(x = carat))
p <- p + layer(
geom = "bar",
geom_params = list(fill = "steelblue"),
stat = "bin",
stat_params = list(binwidth = 0.5)
)
p
#basic form
#geom_XXX(mapping, data, ..., geom, position)
#stat_XXX(mapping, data, ..., stat, position)
#Example 1
ggplot(msleep, aes(sleep_rem / sleep_total, awake)) + geom_point()
#is equivalent to
qplot(sleep_rem / sleep_total, awake, data = msleep)
#Example 2
qplot(sleep_rem / sleep_total,
awake, data = msleep,
geom = c("point", "smooth"))
#is equivalent to
ggplot(msleep, aes(sleep_rem / sleep_total, awake)) + geom_point() + geom_smooth()
#is equivalent to
plot = ggplot(msleep, aes(sleep_rem / sleep_total, awake))
plot = plot + geom_point() + geom_smooth()
plot
#Example 3 (cont.)
bestfit = geom_smooth(method = "lm",
se = T,
colour = "steelblue",
alpha=0.5,
size = 2)
qplot(sleep_rem, sleep_total, data = msleep) + bestfit
qplot(displ, hwy, data=mpg, facets = . ~ year) + bestfit
#Transform the data in the plot using %+%
p = ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point(size=5)
p
mtcars = transform(mtcars, mpg = mpg ^ 2)
p %+% mtcars
#Aesthetic mappings
qplot(color, price, data = diamonds, geom = "boxplot",fill=I("blue"))
#Aesthetic mappings
library(ggplot2)
#Plots and Layers
p = ggplot(mtcars, aes(x = mpg, y = wt))
p + geom_point()
p + geom_point(aes(colour = factor(cyl)))
p + geom_point(aes(y = disp))
#Add aes(colour = cyl) ======> aes(mpg, wt, colour = cyl)
#Override aes(y = disp) ======> aes(mpg, disp)
#Remove aes(y = NULL) ======> aes(mpg)
#Setting vs. mapping
# Mapping: aes(colour = cut))
# Setting: color = "red"
p + geom_point(colour = "green")
# is different from
p + geom_point(aes(colour = "blue"))
#When "green" is mapped to colour, it is treated
#as a regular value and scaled with the default colour scale.
#Grouping
library(nlme)
data(package="nlme")
?Oxboys
head(Oxboys)
str(Oxboys)
p = ggplot(Oxboys, aes(age, height, group = Subject)) + geom_line()
ggplot(Oxboys, aes(age, height, group = 1)) + geom_line()
#Different groups on different layers
p + geom_smooth(aes(group = Subject), method="lm", se = F)
p + geom_smooth(aes(group = 1), method="lm", se = F, size=2)
#Overriding the default grouping.
#no need to specify the group aesthetic here; the default grouping
#works because occasion is a discrete variable
boysbox = ggplot(Oxboys, aes(Occasion, height)) + geom_boxplot()
boysbox
boysbox + geom_line(aes(group = Subject), colour = "blue")
#Geom
#Stat
#Position adjustments
#dodge: Adjust position by dodging overlaps to the side
#fill: Stack overlapping objects and standardise have equal height
#identity: Do not adjust position
#jitter: Jitter points to avoid overplotting
#stack: Stack overlapping objects on top of one another
ggplot(diamonds,
aes(clarity, fill = cut)) + geom_bar(position = "stack")
ggplot(diamonds,
aes(clarity, fill = cut)) + geom_bar(position = "fill")
ggplot(diamonds,
aes(clarity, fill = cut)) + geom_bar(position = "dodge")
第三课 qplot作图
#
plot(mtcars$wt,mtcars$mpg,type ="l")
#
hist(mtcars$mpg,breaks =10)
#
boxplot(mtcars$mpg,pars = list(boxwex = 0.4, staplewex = 0.9, outwex = 0.5))
#plotting a Function Curve
curve(x^3-5*x,form =-4,to =4)
myfun <- function(xvar){
1/(1+exp(-xvar +10))
}
curve(myfun(x),form =0,to=20)
#
qplot(carat,price,data =diamonds,shape=cut,colour=color)
#geom 增添一些功能
setwed("workplace")
第四课 ggplot作图
ggplot相对于qplot的优势在于aes(添加属性)、layer等修饰操作,geom和stat基本上是知道与不知道的问题。同一张图代码写法可能有多重,You are limited by your imagination.
#柱状图
qplot(color,data =diamonds,geom="bar",fill=color)
#线型图
qplot(carat,data =diamonds,geom="histogram",colour=color)
#热图
清洗数据存入csv
str()
rownames()
colnames()
library(gplots)
getwd()
x <-read.csv("NodGenes.csv”,check.name =FALSE)
y <-data.matrix(NodGenes)
heatmap.2(y,main ="Sample",trace ="none",margins= c(10,12),cexRow =0.5,Rowv= FALSE,Colv=FALSW,Key =FALSE)
yb <-colorRampPalette(c("yellow","white","darkblue"))
heatmap.2(y,col =yb, main ="Sample",trace ="none",margins= c(10,12),cexRow =0.5,Rowv= FALSE,Colv=FALSW,Key)
第五课 其他生物信息相关图
#相关图
#地图
#饼图
#聚类图
#曼哈顿图
#基因分布图
#群体结构分布图
#基因组共线性图
#LD图
#Circos图
相关文章推荐
- 停止更新支持的Ubuntu更新源
- Spring MVC,拦截器实现session控制
- javaweb学习总结(八)——HttpServletResponse对象(二)
- 第三周学习进度条
- 在MacOS下Python安装lxml报错找不到头文件 xmlversion.h 的解法方法
- 关于动态插件化技术的网站
- Intellij IDEA快捷键
- PaaS优点与限制(3)
- dijkstra算法的应用(poj2387)
- PaaS优点与限制(3)
- 项目常用Javascript分享,包含常用验证和Cookie操作
- Scala学习笔记(2)
- 致我们终将失去的CF
- adb和samba和struts2的关联
- linux内核分析第四周-使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
- linux网络文件共享服务的实现
- Scala学习笔记(1)
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- 改变状态栏的颜色
- 稍微码一下蒟蒻的链剖序+线段树 大概是模板吧……