R in action读书笔记(21)第十六章 高级图形进阶(上)
16.1 R 中的四种图形系统
基础图形函数可自动调用,而grid和lattice函数的调用必须要加载相应的包(如library(lattice))。要调用ggplot2函数需下载并安装该包(install.packages("ggplot2")),第一次使用前还要进行加载(library(ggplot2))。
16.2 lattice 包
lattice包为单变量和多变量数据的可视化提供了一个全面的图形系统。在一个或多个其他变量的条件下,栅栏图形展示某个变量的分布或与其他变量间的关系。
> library(lattice)
> histogram(~height|voice.part,data=singer,main="Distributionof heights by voice pitch",xlab="height (inches)")
lattice包提供了丰富的函数,可生成单变量图形(点图、核密度图、直方图、柱状图和箱线图)、双变量图形(散点图、带状图和平行箱线图)和多变量图形(三维图和散点图矩阵)。各种高级绘图函数都服从以下格式:
graph_function(formula,data=,options)
graph_function是某个函数。
formula指定要展示的变量和条件变量。
data指定一个数据框。
options是逗号分隔参数,用来修改图形的内容、摆放方式和标注。
lattice中高级绘图函数的常见选项
lattice绘图示例:
> gear<-factor(gear,levels=c(3,4,5),labels=c("3 gears","4 gears","5 gears"))
> cyl<-factor(cyl,levels=c(4,6,8),labels=c("4 cylinders","6 cylinders","8 cylinders"))
> densityplot(~mpg,main="Density plot",xlab="miles per gallon")
> densityplot(~mpg | cyl,main="Density plot by number of cylinders",xlab="miles per gallon")
> bwplot(cyl~mpg|gear,main="box plots by cylinders and gears",xlab="carweight",ylab="cylinders")
> xyplot(mpg~wt|cyl*gear,main="scatter plots by cylinders and gears",xlab="car weight",ylab="miles per gallon")
> cloud(mpg~wt*qsec|cyl,main="d scatter plots by cylinders")
> dotplot(cyl~mpg|gear,main="dot plots by number of gears and cylinders",xlab="miles per gallon"
> splom(mtcars[c(1,4,5,6)],main="scatter plot matrix for mtcars data")
16.2.1 条件变量
> myshingle<-equal.count(x,number=#,overlap=proportion)
将会把连续型变量x分割到#区间中,重叠度为proportion,每个数值范围内的观测数相等,并返回为一个变量myshingle(或类shingle)。输出或者绘制该对象(如plot(myshingle))将会展示瓦块区间。一旦一个连续型变量被转换为一个瓦块,你便可以将它作为一个条件变量使用。
> displacement<-equal.count(mtcars$disp,number=3,overlap=0)
> xyplot(mpg~wt|displacement,data=mtcars,
+ main="miles per gallon vs weight by engine displacement",
+ xlab="weight",ylab="miles per gallon",
+ layout=c(3,1),aspect=1.5)
16.2.2 面板函数
每个高级绘图函数都调用了一个默认的函数来绘制面板。这些默认的函数服从如下命名惯例:panel.graph_function,其中graph_function是该水平绘图函数。如:xyplot(mpg~wt|displacement,data=mtcars)也可以写成:xyplot(mpg~wt|displacement,data=mtcars,panel=panel.xyplot)。自定义面板函数的xyplot:
>displacement<-equal.count(mtcars$disp,number=3,overlap=0) > mypanel<-function(x,y){ + panel.xyplot(x,y,pch=19) + panel.rug(x,y) + panel.grid(h=-1,v=-1) + panel.lmline(x,y,col="red",wd=1,lty=2) + } >xyplot(mpg~wt|displacement,data=mtcars, + layout=c(3,1), + aspect=1.5, + main="miles per gallon vs weightby engine displacement", + xlab="weight",ylab="miles per gallon", + pannel=mypanel)
自定义面板函数和额外选项的xyplot
> library(lattice) >mtcars$transmission<-factor(mtcars$am,levels=c(0,1), + labels=c("Automatic","Manual")) > panel.smoother<-function(x,y){ + panel.grid(h=-1,v=-1) + panel.xyplot(x,y) + panel.loess(x,y) + panel.abline(h=mean(y),lwd=2,lty=2,col="green") + } > xyplot(mpg~disp|transmission,data=mtcars, + scales=list(cex=.8,col="red"), + panel=panel.smoother, + xlab="displacement",ylab="miles per gallon", + main="mpg vs displacement bytransmission typr", + sub="dotted lines are group means",aspect=1)
16.2.3 分组变量
当一个lattice图形表达式含有条件变量时,将会生成在该变量各个水平下的面板。若你想将结果叠加到一起,则可以将变量设定为分组变量(grouping variable).
> library(lattice) >mtcars$transmission<-factor(mtcars$am,levels=c(0,1), + labels=c("Automatic","Manual")) > densityplot(~mpg,data=mtcars, + group=transmission, + main="mpg distributuion bytransmission type", + xlab="miles per gallon", + auto.key=TRUE)
对图例进行更多的控制,可使用key =选项
> library(lattice) > mtcars$transmission<-factor(mtcars$am,levels=c(0,1), + labels=c("Automatic","Manual")) >colors=c("red","blue")#设定颜色、线和点类型 > lines=c(1,2) > points=c(16,17) >key.trans<-list(title="Transmission",#自定义图例 + space="bottom",columns=2, + text=list(levels(mtcars$transmission)), + points=list(pch=points,col=colors), + lines=list(col=colors,lty=lines), + cex.title=1,cex=.9) >densityplot(~mpg,data=mtcars, + group=transmission, + main="mpg distributuion bytransmission type", + xlab="miles per gallon", + pch=points,lty=lines,col=colors,#自定义密度图 + lwd=2,jitter=.005, + key=key.trans)
包含分组变量和条件变量以及自定义图例的xyplot
> library(lattice) >colors="darkgreen" > symbols<-c(1:12) > linetype<-c(1:3) > >key.species<-list(title="plant", + space="right", + text=list(levels(CO2$Plant)), + points=list(pch=symbols,col=colors)) > >xyplot(uptake~conc|Type*Treatment,data=CO2, + group=Plant, + type="o", + pch=symbols,col=colors,lty=linetype, + main="carbon dioxide uptake\ningrass plants", + ylab=expression(paste("Uptake", + bgroup("(",italic(frac("umol","m"^2)),")"))), + xlab=expression(paste("Concenteation", + bgroup("(",italic(frac(mL,L)),")"))), + sub="Grass species:echinochloacrus-gailli", + key=key.species)
R in action读书笔记(21)第十六章 高级图形进阶(上)的更多相关文章
- R in action读书笔记(22)第十六章 高级图形进阶(下)
16.2.4 图形参数 在lattice图形中,lattice函数默认的图形参数包含在一个很大的列表对象中,你可通过trellis.par.get()函数来获取,并用trellis.par.set() ...
- R in action读书笔记(12)第九章 方差分析
第九章方差分析 9.2 ANOVA 模型拟合 9.2.1 aov()函数 aov(formula, data = NULL, projections =FALSE, qr = TRUE, contra ...
- R in action读书笔记(8)-第八章:回归(上)
8.1回归的多面性 8.2 OLS回归 OLS回归拟合模型形式: 为了能够恰当地解释oLs模型的系数,数据必须满足以下统计假设. 口正态性对于固定的自变量值,因变量值成正态分布. 口独立性Yi值之间相 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十六章:实例化和截头锥体裁切
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十六章:实例化和截头锥体裁切 代码工程地址: https://git ...
- UNP学习笔记(第二十六章 线程)
线程有时称为轻权进程(lightweight process) 同一进程内的所有线程共享相同的全局内存.这使得线程之间易于共享信息,然后这样也会带来同步的问题 同一进程内的所有线程处理共享全局变量外还 ...
- R in action读书笔记(19)第十四章 主成分和因子分析
第十四章:主成分和因子分析 本章内容 主成分分析 探索性因子分析 其他潜变量模型 主成分分析(PCA)是一种数据降维技巧,它能将大量相关变量转化为一组很少的不相关变量,这些无关变量称为主成分.探索性因 ...
- R in action读书笔记(16)第十二章 重抽样与自助法之 置换检验
第十二章:重抽样与自助法 本章,我们将探究两种应用广泛的依据随机化思想的统计方法:置换检验和自助法 12.1 置换检验 置换检验,也称随机化检验或重随机化检验. 有两种处理条件的实验,十个受试者已经被 ...
- WPF,Silverlight与XAML读书笔记第四十六 - 外观效果之三皮肤与主题
说明:本系列基本上是<WPF揭秘>的读书笔记.在结构安排与文章内容上参照<WPF揭秘>的编排,对内容进行了总结并加入一些个人理解. 皮肤 皮肤是应用程序中样式与模板的集合,可以 ...
- 《深入理解java虚拟机》读书笔记十一——第十二章
第十二章 Java内存模型与线程 1.硬件效率与一致性 由于计算机的存储设备与处理器的运算速度有几个数量级的差距,所以现代计算机系统都不得不加入一层读写速度尽可能接近处理器运算速度的高速缓存(Cac ...
随机推荐
- Django模板语言(二)
1,装饰器:在不改变原函数的调用方式情况下为原函数增加一些功能(遵循开放封闭的原则) def outter(fn): def inner(*args, **kwargs): # 可以在执行函数前执行一 ...
- 搭建nodejs服务,访问本地站点文件
搭建nodejs服务器步骤: 1.安装nodejs服务(从官网下载安装) 2.在自己定义的目录下新建服务器文件如 server.js 例如,我在E:\PhpProject\html5\websocke ...
- MySql安装与使用图文教程
2.下载完成后将其解压到你想要安装的路径下,例如我的解压到D:\MySql\mysql-5.7.12-winx64\路径下,刚解压完应该是下图这些文件夹:最好解压到根目录. 5.新建一个my.in ...
- Masonry整体动画更新约束
前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...
- CF上的3道小题(2)
CF上的3道小题(2) T1:CF630K Indivisibility 题意:给出一个数n,求1到n的数中不能被2到9中任意一个数整除的数. 分析:容斥一下,没了. 代码: #include < ...
- 【ZJOI 2008】 生日聚会
[题目链接] 点击打开链接 [算法] 动态规划 f[i][j][x][y]表示当前选了i个男生,j个女生,男生与女生差最大为x,女生与男生差最大为y的方案数 转移很显然,笔者不再赘述 [代码] #in ...
- Android 布局学习之——LinearLayout属性baselineAligned的作用及baseline(转载)
转自:http://www.cnblogs.com/JohnTsai/p/4074643.html 相信大家对LinearLayout已经相当熟悉,但你们是否了解它的属性baselineAligned ...
- ubuntu /etc/profile和/etc/environment的比较 (转载)
转自:http://blog.csdn.net/teamlet/article/details/8257853 先将export LANG=zh_CN加入/etc/profile ,退出系统重新登录, ...
- Linux 系统管理命令 - mpstat - CPU信息统计
命令详解 重要星级: ★★★★☆ 功能说明: mpstat 是 Multiprocessor Statistics 的缩写,是一种实时系统监控工具.mpstat 命令会输出 CPU 的一些统计信息,这 ...
- 深入浅出索引--Mysql45讲笔记记录 打卡day3
看了极客时间的mysql45讲记录一下自己理解的关于索引部分 为什么会有索引呢? 答:索引就像书的目录一样,可以让你快速知道你要看的部分在多少页.换句话说,索引就是为了提高数据库的查询效率. 索引的数 ...