R语言入门班

来源:互联网 发布:支付宝与淘宝解绑不了 编辑:程序博客网 时间:2024/04/30 22:33

感谢: 吴明昊老师 Swirl包作者 《Elegant Graphics for Dataanlysis》书作者 《R Graphics Cookbook》书作者

第一课  R初体验(12 h)1、安装R2、安装RStudio3.在RStudio下输入以下命令:install.packages("swirl")安装Swirl4、调用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 =" ")plotmathR中缺失值处理NANaNTo 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) <- cnamesR中逻辑表达式allanyR中功能R中申请和供给lapplysapplyvapply and tapplyflips <- sample(c(0,1), 100, replace = TRUE, prob = c(0.3, 0.7))模拟仿真3D图包 (latticeExtra)第二课 R基本语法youtube学习ggplot2:Elegant Graphics for Dataanlysishttp://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 Plotplot(mtcars$wt, mtcars$mpg)#Line Graphplot(pressure$temperature, pressure$pressure, type="l")points(pressure$temperature, pressure$pressure) #add pointslines(pressure$temperature, pressure$pressure/2, type="l",col="red")points(pressure$temperature, pressure$pressure/2,col="red")#Bar Graphbarplot(table(mtcars$cyl))#Histogramhist(mtcars$mpg, breaks=10)#Box Plotboxplot(mtcars$mpg)#Plotting a Function Curvecurve(x^3 - 5*x, from=-4, to=4)myfun <- function(xvar) {  1/(1 + exp(-xvar + 10))}curve(myfun(x), from=0, to=20)#Basic usedat=diamondsqplot(carat, price, data = diamonds)qplot(log(carat), log(price), data = diamonds)qplot(carat, x * y * z, data = diamonds)#Colour, size, shape and other aesthetic attributesdsmall = 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 1qplot(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, fillqplot(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 + 1900qplot(unemploy / pop, uempmed, data = economics,      geom = "path", colour = year(date))#Lecture 2#minghaowu_2015@163.comsetwd("~/documents/R programming/ggplot2")library(ggplot2)attach(mpg)head(mpg)#Reviewqplot(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()#Savep = 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 layerp = ggplot(diamonds, aes(carat, price, colour = cut))pp = 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 1ggplot(msleep, aes(sleep_rem / sleep_total, awake)) + geom_point()#is equivalent toqplot(sleep_rem / sleep_total, awake, data = msleep)#Example 2qplot(sleep_rem / sleep_total,       awake, data = msleep,      geom = c("point", "smooth"))#is equivalent toggplot(msleep, aes(sleep_rem / sleep_total, awake)) + geom_point() + geom_smooth()#is equivalent toplot = 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) + bestfitqplot(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)pmtcars = transform(mtcars, mpg = mpg ^ 2)p %+% mtcars#Aesthetic mappingsqplot(color, price, data = diamonds, geom = "boxplot",fill=I("blue"))#Aesthetic mappingslibrary(ggplot2)#Plots and Layersp = 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 fromp + 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.#Groupinglibrary(nlme)data(package="nlme")?Oxboyshead(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 layersp + 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 variableboysbox = ggplot(Oxboys, aes(Occasion, height)) + geom_boxplot()boysboxboysbox + 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 anotherggplot(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 Curvecurve(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)#热图清洗数据存入csvstr()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图


0 0
原创粉丝点击