R language Note R set graphical parameters--function par () detailed

Source: Internet
Author: User
Tags in degrees

R has a very powerful drawing function, we can draw a variety of graphics with a few simple lines of code, but sometimes the default graphics settings do not meet our needs, and even encounter a variety of small problems: such as the axis or the title is out of bounds, or the size or position of the legend is obscured by the figure, Even sometimes the default color is not enough to meet our needs. How do you make adjustments? This uses the "powerful" function par (). We can adjust our graph by setting the parameters of the function par (), which is a summary of each parameter of the function par ().

The function par () is used in the following format:

Par (..., no.readonly = FALSE)

where ... Represents all parameters similar to the Tag=value form. These parameters are described in detail below. When the parameter is no.readonly=true, the function par () only allows this parameter, and returns the parameter values for each parameter in the current drawing device.

Each graphic device has its own drawing parameters, and if the drawing device is not currently open, the function par () automatically opens a new drawing device before the parameter is set.
As mentioned earlier, enter the command directly in the R editor par () or par (no.readonly=true) can get the current individual drawing parameters.
The parameters in the function par () can be divided into three main categories:

    1. can only be read and cannot be set. Including the parameter cin, cra, csi, cxydin。

    2. 只能通过函数par()进行设置。包括参数:


    • "ask",

    • "fig""fin",

    • "lheight",

    • "mai""mar""mex""mfcol""mfrow""mfg",

    • "new",

    • "oma""omd""omi",

    • "pin""plt""ps""pty",

    • "usr",

    • "xlog","ylog"

The remaining parameters, in addition to the function par (), can also be set through a variety of advanced drawing functions, such as function plot,points,lines,abline,title,text,axis,image,box,contour,rect,arrows.

When the value of a parameter is set, the default returns the value of the parameter before the setting, and we can usually save the values by some variables. After you have done this, you can use these historical values for recovery settings (but this is not recommended because some parameter value conflicts may occur). Such as:

> X<-par (bg= "Red")
> x
$bg
[1] "Transparent"

> Plot (1:10)
> par (x)
> Plot (1:100)

The next step is to explain the meaning of each parameter, using some simple code as an example.


      • adj。该参数值用于设定在text、mtext、title中字符串的对齐方向。0表示左对齐,0.5(默认值)表示居中,而1表示右对齐(说明一下,区间[0,1]内的任何值都可以作为参数adj的有效值,并且在大部分的图形设备中,介于区间外的值也是有效的)。注意一下,函数text中的参数adj的值可以以类似于形式adj=c(x,y)调整方向。但是在text中该参数的值影响的是对点的标记,对函数mtext和title来说,参数adj的值影响的是整个图像或设备区域。

      • > par(mfrow=c(1,2))
        > plot(1:4)
        > title("plot(1:4)",adj=0)
        > plot(1:4)
        > title("plot(1:4)",adj=1)

    • ann。如果ann=FALSE,那么高水平绘图函数会调用函数plot.default使对坐标轴名称、整体图像名称不做任何注解。默认值为TRUE。

> Plot (1:4,ann=false,main= ' plot (1:4,ann=false) ') #尽管指定了参数main的值, but still not displayed in the image
> Plot (1:4,ann=true,main= ' plot (1:4,ann=false) ')

  • ask,逻辑值。若为TRUE(且当前的R会话是可交互状态),则在绘制新图像之前会要求用户输入确认信息。同样的,会对扩展包grid和lattice的输出有影响,甚至可能会应用到没有屏幕输出的设备上(但可能会没有效果)。

  • bg。用于设定绘图区域的背景颜色。当通过函数par()调用时,会同时设定参数new=FALSE。对很多设备来说,该参数的初始值就是该设备的背景颜色值,其他情况下一般为"white"。需要注意一点的是,一些图形函数例如plot.default和points等也有名为bg的参数,但是代表的含义是不同的。

  • bty。该参数值为字符串型,用于限定图形的边框类型。如果bty的值为"o"(默认值)、"l"、"7"、"c"、"u"或者"]"中的任意一个,对应的边框类型就和该字母的形状相似。如果bty的值为"n",表示无边框。

  • cex。用于表示对默认的绘图文本和符号放大多少倍。需要注意一些绘图函数如plot.default等也有一个相同名字的参数,但是此时表示在函数par()的参数cex的基础上再放大多少倍,此外还有函数points等接受一个数值向量为参数。

  • cex.axis。表示在当前的cex设定情况下,对坐标轴刻度值字体的放大倍数。

  • cex.lab。表示在当前的cex设定情况下,对坐标轴名称字体的放大倍数。

  • cex.main。表示在当前的cex设定情况下,对主标题字体的放大倍数。

  • cex.sub。表示在当前的cex设定情况下,对子标题字体的放大倍数。

  • cin。这是一个只读参数,不能进行修改。以形式(width,height)返回字体大小,单位为英寸。这和参数cra的作用一样,只是测量单位不同。

  • col。用于设定默认的绘图颜色(无可否认这是一个非常特别的参数,以后会总结博文专门讨论如何设定颜色650) this.width=650; "src=" Http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___7396ZH00SIGG.gif "alt=" Set graphics parameters in R--function par () detailed "title=" R set graphical parameters--function par () detailed "style=" margin:0px;padding:0px;border:0px;list-style:none; "/>)。

  • col.axis。坐标轴刻度值的颜色,默认为"black"。如代码:

>par(cex.axis=3,col.axis="red")

>plot(1:4)

    • Col.lab. The color of the axis name, which defaults to "black".

    • col.main. The color of the main title, which defaults to "black".

    • col.sub. The color of the subtitle, which defaults to "black".

    • CRA. See the description of the parameter cin.

    • crt. The value of this parameter is a numeric value that represents the degree of rotation, preferably a multiple of 90. Unlike the parameter SRT, the latter is the rotation of the entire string.

    • CSI. Read-only parameter, which returns the default character height, in inches.

    • cxy. The read-only parameter, in form (Width,height), returns the default character width, height, where par ("cxy") =par ("cin")/par ("pin").

    • din. A read-only parameter that represents the size specification of the drawing device, returned in form (width,height), in inches.

    • family. The font type of the characters in the drawing. The maximum length is bytes. The default value is "", which means the default font for the drawing device is used.

    • FG. Colors that are prominent in the graph (such as axes, tick marks, borders, and so on) are typically "black" by default.

    • Fig. A numerical vector, in the form C (x1, x2, y1, y2), used to set the current drawing in the drawing device occupies the area, attention needs to meet the x1<x2,y1<y2. If you modify the parameter fig, a new drawing device is opened automatically, and if you want to add a new graphic to the original drawing device, you need to use it with the parameter new=true. such as code:

;  par (Fig=c ( 0,0.5,0,0.5)
,  plot (1:3)

;  par (Fig=c (0.5,1,0.5,1 )
,  plot (1:3)

;  par (Fig=c (0,0.5,0,0.5), new=true)

;  plot (1:3)

    • Fin The size specification for the current drawing area, in the form (width,height), in inches. If you modify the value of this parameter, the default is to start a new drawing device.

    • Font Used to set what type of font to use, as an integer. 1 for plain text (default), 2 for bold, 3 for italic, and 4 for black italic. Under Adobe character encoding, 5 is also available.

    • Font.axis. The font of the axis scale value.

    • Font.lab. The font of the axis name.

    • Font.main. The font of the main title.

    • Font.sub. The font of the sub-title.

    • Lab A numeric vector, expressed in form C (X,y,len), used to set the name of the axis. Values x and Y are used to set the number of tick marks on the x and Y axes, and Len sets the length of the tick mark (this value in R currently has no effect).

    • Las. Can only be a value in 0,1,2,3, which represents the direction of the scale value. 0 means always parallel to the axis, 1 is always horizontal, 2 is always perpendicular to the axis, and 3 is always vertical.

    • Lend The end style of the line segment, which can be an integer or a string. A parameter value of 0 or "round" indicates that the endpoint style is rounded (the default), or 1 or "butt", indicating that the endpoint is truncated directly, or that the extension ends for 2 or "square". such as code:

>Plot (1:8,type= "n", Ylim=c (1,6))
>X0<-c (1,2,3,5,6)
>Y0<-rep (1,5)
>X1<-x0
>Y1<-rep (6,5)
>Segments (X0,Y0,X1,Y1)
>X0<-rep (0,4)
>Y0<-seq (1.3,4.3,1)
>X1<-c (1,6,3,1)
>Y1<-y0
#默认情形
>Segments (x0,y0,x1,y0,lwd=10,col= "Red")
#修改参数lend =1
>Y0<-seq (1.6,4.6,1)
>Y1<-y0
>Segments (x0,y0,x1,y0,lwd=10,col= "Green", lend=1)
#修改参数lend =2
>Y0<-seq (1.9,4.9,1)
>Y1<-y0
>X0<-x1
>X1<-c (2,8,5,3)
>Segments (x0,y0,x1,y0,lwd=10,col= "Blue", lend=2)

  • Lty. Line type. The values of the arguments can be integers (0 is empty, 1 is solid (the default), 2 is dashed, 3 is dotted, 4, 5, 6, and so on), or it can be a string (and the integer is one by one, such as "blank", "solid", "dashed"、 "dotted"、 "dotdash"、 "longdash" or "twodash" ).

  • Lwd Line width. Must be an integer with a default value of 1. The specific implementation depends on the device, and some drawing devices do not support line widths less than 1.

  • Mfcol,mrow. Used to set the layout of the image device (simply by separating the current drawing device into a NR*NC sub-device), with the Parameter form C (NR, NC). The plot order of the sub-graph is either Mfcol or Mfrow, depending on whether the parameter is specified by column or by row. You can also use function layout or split.screen to achieve the same functionality.

  • Mgp Sets the distance between the title, axis name, and axis distance from the drawing border. The default value is C (3,1,0), where the first value affects the caption.

  • New Logical value, the default value is False. If set to true, the next Advanced drawing command does not empty the current drawing device.

  • Oma. The Parameter form is C (bottom, left, top, right), which is used to set the outer boundary.

  • Omi As with the parameter OMA, only the units of this parameter are in inches.

  • Pch. A shape that represents a point whose value can be numeric (a numeric value between 0 and 25) or a character type. 1 represents a circle, 2 represents a triangle, and 3 represents a "+" sign. When the value of the PCH is a character type, then the point is represented by that character.

  • Pin The current dimension, in Form C (width,height), in inches.

  • Plt. In the form c(x1, x2, y1, y2) , set the current drawing area.

  • Pty A character parameter that represents the shape of the current drawing area, and "s" means that a square area is generated, and "M" represents the largest drawing area.

  • Srt. string rotation in degrees, only the function text is supported.

  • Tck The length of the tick mark, which is a fraction less than or equal, represents a portion of the height or width of the drawing area (whichever is smaller in height or width). If tck=1, the grid lines are drawn. The default value is Na (equivalent to tcl=-0.5).

  • Tcl. It can also be used to set the length of a tick mark, but differs from the unit of TCK. Its default value is-0.5.

  • Usr. A vector of form C (x1, x2, y1, y2) that represents the range of coordinate values for the current drawing area: C (Xleft, Xright, Ybottom, ytop). If the logarithmic scale is used (such as par ("xlog") =true), then the x-axis representation range is 10^par ("usr") [1:2], and the y-axis can also be represented.

  • Xaxp. A vector of form C (x1, x2, N) that represents the number of tick marks in the x-axis and the range of ticks in the interval when par ("Xlog") is =false. If par ("xlog") =true, the situation is slightly more complicated: if the range of values is small, then n is a negative number, and the distribution of tick marks is similar to normal (no logarithmic conversion), if n is a value of 1, 2, 3, C (x1,x2) =10^par ("usr") [1:2] (and at this time par ("usr") refers to the value returned by par ("xlog") =true case). The specific explanations are as follows:

N=1, draws a tick mark at a coordinate value of 10^j (j is an integer).

n=2, which draws tick marks at the coordinate value k* (10^j), where k is 1 or 5.

n=3, which draws tick marks at the coordinate value k* (10^j) where k is 1, 2, or 5.

    • Yaxp. Similar to XAXP, indicates the range of tick marks on the Y axis and the number of tick marks in the interval.

    • Xaxs. How to set the interval of Axis x. The values range from: "R",   "I" ,   "E" ,   "S" ,   "D". In general, the calculation is determined by the value range of the Xlim (if Xlim is specified). "R" (regular) will first extend the range of values to each end of the 4%, and then set the coordinate values in the extended range of values, "I" (internal) directly in the original data range set coordinate values, "s" (standard) and "E" (extended),; "D" ( Direct) is not currently supported.

    • yaxs. Similar to XAXS, the interval for axis y is set.

    • xaxt. Used to set the scale value type of the x axis to one character. "N" means no scale values and tick marks; "s" means drawing, default value.

    • yaxt. Similar to Xaxt.

    • xlog. A logical value. If true, indicates that the x-axis is a logarithmic axis, and the default value is False.

    • ylog. Similar to Xlog.

    最后,说明一点:如何设定颜色?R提供了很多和颜色相关的函数供我们调用,如colors()、palette()、rainbow()、rgb()、gray()、hsv()、hcl()等等。以后也会对“如何设定颜色”这一问题进行整理,并且接下来关于R语言的博文也会重点关注于可视化方面,包括如何绘制散点图、条形图、热点图、地图等。


R language Note R set graphical parameters--function par () detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.