R语言画图基础参数设置
2016-01-08 15:00
357 查看
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 usewhen plotting points. For symbols 21 through 25, specify border color
(col=) and fill color (bg=).
![](http://www.statmethods.net/advgraphs/images/points.png)
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. |
![](http://www.statmethods.net/advgraphs/images/lines.png)
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 |
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.
![](http://www.statmethods.net/advgraphs/images/colorchart.png)
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. |
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")
![](http://www.statmethods.net/advgraphs/images/smfonts.jpg)
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 |
Going Further
See help(par) for more information on graphical parameters. The customization of plotting axes and text annotations are covered next section.FROM:
http://www.statmethods.net/advgraphs/parameters.html
相关文章推荐
- Android View 事件分发机制详解
- Linux查看某个文件被哪些进程PID占用
- linux学习笔记----4
- iOS Newbie - Xcode 7 & iOS 9 页面间传值
- 21-《电子入门趣谈》第四章_自己制作电路板-4.2洞洞板的介绍和经典案例使用教程
- Nginx去除版本号
- linux tail命令的使用方法详解(转)
- 带边框的文本框TextView
- 开发APP不搞清楚这20个问题,必然沦为一场灾难
- 经典算法的Python实现(3)
- 异或运算
- 华为机试——求数组中的第一大和第二大数
- Redis常见的性能问题和解决方法
- SpringMVC搭配Freemarker实例解析
- 找到合适的方案记录服务端日志
- 自下而上归并排序 数组实现
- MongoDB基本命令
- Nginx -- Gzip 压缩功能作用
- 并查集(涂色问题) HDOJ 4056 Draw a Mess
- Struts2内建校验器(基于校验框架的文件校验)