R语言画图基础参数设置
Graphical Parameters
You can customize many features of your graphs (fonts, colors, axes, titles) through graphic options.
One way is to specify these options in through the par( ) function. If you set parameter values here, the changes will be in effect for the rest of the session or until you change them again. The format is par(optionname=value, optionname=value, ...)
# Set a graphical parameter using par()
par() # view current settings
opar <- par() # make a copy of current settings
par(col.lab="red") # red x and y labels
hist(mtcars$mpg) # create a plot with these new settings
par(opar) # restore original settings
A second way to specify graphical parameters is by providing the optionname=value pairs directly to a high level plotting function. In this case, the options are only in effect for that specific graph.
# Set a graphical parameter within the plotting function
hist(mtcars$mpg, col.lab="red")
See the help for a specific high level plotting function (e.g.
plot, hist, boxplot) to determine which graphical parameters can be set
this way.
The remainder of this section describes some of the more important graphical parameters that you can set.
Text and Symbol Size
The following options can be used to control text and symbol size in graphs.
option | description |
cex | number indicating the amount by which plotting text and symbols should be scaled relative to the default. 1=default, 1.5 is 50% larger, 0.5 is 50% smaller, etc. |
cex.axis | magnification of axis annotation relative to cex |
cex.lab | magnification of x and y labels relative to cex |
cex.main | magnification of titles relative to cex |
cex.sub | magnification of subtitles relative to cex |
Plotting Symbols
Use the pch= option to specify symbols to use
when plotting points. For symbols 21 through 25, specify border color
(col=) and fill color (bg=).
Lines
You can change lines using the following options. This is particularly useful for reference lines, axes, and fit lines.
option | description |
lty | line type. see the chart below. |
lwd | line width relative to the default (default=1). 2 is twice as wide. |
Colors
Options that specify colors include the following.
option | description |
col | Default plotting color. Some functions (e.g. lines) accept a vector of values that are recycled. |
col.axis | color for axis annotation |
col.lab | color for x and y labels |
col.main | color for titles |
col.sub | color for subtitles |
fg | plot foreground color (axes, boxes - also sets col= to same) |
bg | plot background color |
You can specify colors in R by index, name, hexadecimal, or RGB.
For example col=1, col="white", and col="#FFFFFF" are equivalent.
The following chart was produced with code developed by Earl F. Glynn. See his Color Chart for all the details you would ever need about using colors in R.
You can also create a vector of n contiguous colors using the functions rainbow(n), heat.colors(n), terrain.colors(n), topo.colors(n), and cm.colors(n).
colors() returns all available color names.
Fonts
You can easily set font size and style, but font family is a bit more complicated.
option | description |
font | Integer specifying font to use for text. 1=plain, 2=bold, 3=italic, 4=bold italic, 5=symbol |
font.axis | font for axis annotation |
font.lab | font for x and y labels |
font.main | font for titles |
font.sub | font for subtitles |
ps | font point size (roughly 1/72 inch) text size=ps*cex |
family | font family for drawing text. Standard values are "serif", "sans", "mono", "symbol". Mapping is device dependent. |
In windows, mono is mapped to "TT Courier New", serif is mapped
to"TT Times New Roman", sans is mapped to "TT Arial", mono is mapped to
"TT Courier New", and symbol is mapped to "TT Symbol" (TT=True Type).
You can add your own mappings.
# Type family examples - creating new mappings
plot(1:10,1:10,type="n")
windowsFonts(
A=windowsFont("Arial Black"),
B=windowsFont("Bookman Old Style"),
C=windowsFont("Comic Sans MS"),
D=windowsFont("Symbol")
)
text(3,3,"Hello World Default")
text(4,4,family="A","Hello World from Arial Black")
text(5,5,family="B","Hello World from Bookman Old Style")
text(6,6,family="C","Hello World from Comic Sans MS")
text(7,7,family="D", "Hello World from Symbol")
click to view
Margins and Graph Size
You can control the margin size using the following parameters.
option | description |
mar | numerical vector indicating margin size c(bottom, left, top, right) in lines. default = c(5, 4, 4, 2) + 0.1 |
mai | numerical vector indicating margin size c(bottom, left, top, right) in inches |
pin | plot dimensions (width, height) in inches |
For complete information on margins, see Earl F. Glynn's margin tutorial.
Going Further
See help(par) for more information on graphical parameters. The customization of plotting axes and text annotations are covered next section.
FROM:
R语言画图基础参数设置的更多相关文章
- R 语言画图的基本参数
R 语言画图的基本参数 点 点的种类 点的种类参数为 pch,每一种符号对应一个数字编号 # 点有25种,为了展示25种点 x = 1:25 y = 1:25 x ## [1] 1 2 3 4 5 6 ...
- R语言plot函数参数合集
最近用R语言画图,plot 函数是用的最多的函数,而他的参数非常繁多,由此总结一下,以供后续方便查阅. plot(x, y = NULL, type = "p", xlim = N ...
- R语言语法基础二
R语言语法基础二 重塑数据 增加行和列 # 创建向量 city = c("Tampa","Seattle","Hartford"," ...
- R语言画图教程之盒形图
R语言画图教程之盒形图 我们之前有分享过一系列的R语言画图代码(PCA图.Pathway图.火山图.RDA图.热图),今天再来补充一个盒形图(箱形图)的代码. 以下代码只是示例,不能直接搬来用哦,注意 ...
- R语言语法基础一
R语言语法基础一 Hello world #这里是注释 myString = "hello world" print(myString) [1] "hello world ...
- shell中调用R语言并传入参数的两种步骤
shell中调用R语言并传入参数的两种方法 第一种: Rscript myscript.R R脚本的输出 第二种: R CMD BATCH myscript.R # Check the output ...
- 转载 R语言颜色基础设置
原文链接:http://www.biostatistic.net/thread-5065-1-1.html R语言在画图形的时候,经常遇到颜色设定问题,用户可以根据color.rgb值和hsv值来设定 ...
- R语言编程艺术(3)R语言编程基础
本文对应<R语言编程艺术> 第7章:R语言编程结构: 第9章:面向对象的编程: 第13章:调试 ============================================== ...
- R语言画图小结
本文以1950年到2010年期间我国的火灾统计数据为例,数据如下所示: (0)加载数据 data<-read.csv("E:\\MyDocument\\p\\Data\\1950~20 ...
随机推荐
- 创建DirectInput接口对象说明---(void **)&m_pDI
读别人代码时遇到的,起初没明白过来这是怎么回事,后来忽然想明白了. if (FAILED(DirectInput8Create(appInstance, DIRECTINPUT_VERSION, II ...
- 七、Java基础---------JDK安装与配置
配置环境变量名词说明 path:通过path系统去寻找可执行的java文件. JAVA_HOME:JDK的安装目录 classpath:加载目录 为什么需要配置path,什么时候需要classpath ...
- 【DP】斜率优化
斜率优化 入门题:PKU3709 很多人貌似都是做这道题来K斜率优化的,所以看了资料以后还是开始入手吧. 然而还是得跪求大神的程序啊 ORZ ORZ…… 其实理解斜率优化就是会列斜率不等式,还要理解剔 ...
- FireDac 与数据库连接时字符集及对应的字段类型问题
近日在一个过程调用时发生一个奇怪现象, 异常返回意思是说, 数据的长度是[6], 而字段定义的长度是[3]. 分析后认为: 调用过程你不涉及到对返回数据集的字段手动定义问题, 出现这个问题应是两边 ...
- Linux/Unix中的#!和!#
是不是在Terminal输入命令的时候,输入了很长的一个路径,然后发现还有在同一条命令中再输一次的时候很恼火,其实Shell是提供了trick的,就是使用!#(和#!不同哦) 习惯写脚本的猿,通常对于 ...
- svn down代码的时候提示“由于目标计算机积极拒绝,无法连接”
如上图所示: 原因1:svn没有启动成功 原因2:防火墙没有svn的端口号3690 解决方法: 1.启动svn服务 svnserve -d -r svn路径 例如:svnserve -d -r / ...
- [工具][windows][visualStudio][充电]番茄助手vaassist常见用法
参考:http://blog.csdn.net/hotdog156351/article/details/43955565 1 安装好VAS打开VS2010之后,首先关闭VA outline与VA V ...
- float浮动与清除浮动
浮动: 浮动的框可以左右移动,直到它的边缘碰到包含框或另一个浮动框的边框为止.文档的普通流中的块框会当浮动框不存在一样.但会影响内联框(通常是文本)的排列. 属性值有:left,right,none; ...
- 理解css中的position-static\relative\fixed\absolute
position属性有四个值: static(静态定位):是默认值,不会被特殊的定位,遵循正常的文档流对象,对象占用文档空间,该方式下,top.right.bottom.left.z-index等属性 ...
- 【转】MYSQL入门学习之十三:自定义函数的基本操作
转载地址:http://www.2cto.com/database/201212/177382.html 一.自定义函数(UDF)的特性和功能 www.2cto.com 函数能分 ...