plot function in r

27
DATA VISUALIZATION WITH R: FUNCTION PLOT() Vladimir E. Bakhrushin, Professor, D. Sc. [email protected]

Upload: vladimir-bakhrushin

Post on 18-Dec-2014

153 views

Category:

Education


0 download

DESCRIPTION

Presentation on data visualization with plot() function in R

TRANSCRIPT

Page 1: Plot function in R

DATA VISUALIZATION WITH R: FUNCTION PLOT()

Vladimir E. Bakhrushin,Professor, D. Sc.

[email protected]

Page 2: Plot function in R

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.

Page 3: Plot function in R

Function plot()

Page 4: Plot function in R

Some arguments of the plot() function

Function plot()

pch

type

mainVectors x, y

legend

cex

collty

Page 5: Plot function in R

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.

Page 6: Plot function in R

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.

Page 7: Plot function in R

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".

Page 8: Plot function in R

Graph types

Page 9: Plot function in R

Graph types

Page 10: Plot function in R

Titles and subtitles

Argument main is used for creating graph title. Graph subtitle may be created with argument sub.

Page 11: Plot function in R

Titles and subtitles

Page 12: Plot function in R

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.

Page 13: Plot function in R

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 = "&".

Page 14: Plot function in R

font = 5

Page 15: Plot function in R

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).

Page 16: Plot function in R

Colors

Page 17: Plot function in R

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.

Page 18: Plot function in R

Line types

Page 19: Plot function in R

Line types

Page 20: Plot function in R

Line types

Page 21: Plot function in R

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.

Page 22: Plot function in R

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.

Page 23: Plot function in R

Lines ends

Page 24: Plot function in R

Lines joining

Page 25: Plot function in R

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").

Page 26: Plot function in R

Legend

Page 27: Plot function in R

Література

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