plot function in r
DESCRIPTION
Presentation on data visualization with plot() function in RTRANSCRIPT
DATA VISUALIZATION WITH R: FUNCTION PLOT()
Vladimir E. Bakhrushin,Professor, D. Sc.
Function plot()
Function plot() is the main tool for 2D plotting in R.
If we have two vectors x, y of equal length, function plot(x,y) allows us to get the graph of y(x) dependence.
If only one variable is given, we will get a graph for which set vector provide the ordinate values and the points ordinal numbers are abscissas.
Function plot()
Some arguments of the plot() function
Function plot()
pch
type
mainVectors x, y
legend
cex
collty
Combining plots
There is an opportunity to combine several plots into one overall graph using commands:
old.par <- par(mfrow=c(n,m)) …par(old.par)
First command places in the window nm plots (m horizontally and n vertically).
The last command restores the default graphical parameters. Between them we have to specify what charts must be built.
Graph types
Argument type give us an opportunity to change the view of graph. It can take the following text values:
"p" – points (default option);"l" – lines;"b" – lines between points;"c" - lines part alone of "b";"o" – points over lines; "h" – histogram;"s", " S" – step curves ;"n" – no points and lines.
Arguments of the plot() function
xlab and ylab – assignment of titles to the coordinate axes, for example: xlab = "Year", ylab = "Gross domestic product"
xlim and ylim – setting the ranges of values displayed on the abscissa and ordinate axes, respectively; default values of these arguments are NULL
axes and ann – control of displaying axes and their names; their values may be "TRUE" or "FALSE"
log – graph in logarithmic or semilogarithmic scale; it may take values "x", "y" or "xy".
Graph types
Graph types
Titles and subtitles
Argument main is used for creating graph title. Graph subtitle may be created with argument sub.
Titles and subtitles
Point character
To set point character we can use argument pch.
First way is to assign a numerical value to argument pch. It may be taken from the interval 1 – 25 or 32 – 255.
Numbers from 32 to 127 correspond to ASCII symbols, for example: 33 – !, 34 – ", 35 – # etc. Numbers from 128 to 255 give an opportunity to use some other symbols, such as Cyrillic alphabet letters, gothic and so on.
Point characters
Another way of setting point characters is combining of arguments pch and font. The last defines a font of characters.
Default option is font = 1. For the most of other font values we obtain different variants of writing the same symbols as in the absence of this argument. For example, at font = 2 bold characters are used, at font = 3 – italic, at font = 4 – bold italic etc.
Also we can specify character type directly, for example: pch = "&".
font = 5
Colors
To set the color of characters and some other elements we can use argument col. Also in R there is a possibility to set colors of individual chart elements. For example, col.main sets the title color, col.lab – names of axes, col.axes – axes annotations etc.).
Here are the main ways to set colors: by name we can set 675 standard colors, for example: col = "red"; all color names may be obtained with command colors(); by numeric code, for example: col = 3 (green); by RGB spectrum components, for example: col = "#FF6347" (tomato).
Colors
Lines
For setting line type argument lty is used. There are 6 predetermined line types.
If it is necessary you can create your own line types. In this case, the value of the argument lty is the text sequence of four numbers from 1 to 9, for example: lty = "4241". This line consists of the pattern, which includes two dashes of four units in length and spaces which lengths are alternately equal to two or one unit.
Line width may be set with argument lwd.
Line types
Line types
Line types
Lines ends and joining
Argument lend is used for setting the form of line ends. It may take such values:
0 (default) – rounded ends; 1 – lines ends are straight and perpendicular to lines; 2 – lines ends are straight and parallel to the coordinate
axes.
Form of lines joining is set with the argument ljoin. It may take such values:
0 (за замовченням) – rounded joining; 1 – sharp-cornered joining; 2 – truncated joining.
Lines ends and joining
Note that the need to hide the names of the axes in the second graph is enough to specify only when setting the first line.
Lines ends
Lines joining
Legend
To create the legend function legend() may be used.Its arguments are: coordinates x and y of the legend
window, vector of the individual lines names, vectors indicated colors, point characters, line types etc which correspond to these lines. Argument bty indicates that it is necessary to create a frame around the legend (value "y"), or that a frame should not be created (value "n").
Legend
Література
1.An Introduction to R2.Kelly Black. R Tutorial3.Tutorial: Using plot() function4.R Documentation: Generic X-Y Plotting5.SPLUS/R Library: Notes From Roger's Seminars on R.
Graphing in R