Category «R Graphics»

Greek letters in R plot label and title

Question: How one can include Greek letter (symbols) in R plot labels?
Answer: Greek letters or symbols can be included in titles and labels of graph using the expression command. Following are some examples

Note that in these example random data is generated from normal distribution. You can use your own data set to produce graphs that have symbols or Greek letters in their labels or titles.

Example 1:

> mycoef <- rnorm (1000)
> hist(mycoef, main = expression(beta) )

where beta in expression is Greek letter (symbol) of \beta. A histogram similar to following will be produced.

greek symbols in r plot-1

Example 2:

sample <- rnorm(mean=5, sd=1, n=100)
> hist(sample, main=expression( paste(“sampled values, “, mu, “=5, “, sigma, “=1” )))

where mu and sigma are symbols of \mu and \sigma respectively. Now histogram will look like

greek symbols in r plot-2Example 3:

curve(dnorm, from= -3, to=3, n=1000, main=”Normal Probability Density Function”)

will produce curve of Normal probability density function ranging from -3 to 3.

greek symbols in r plot-3To add normal density function formula, we need to use text and paste command, that is

> text(-2, 0.3, expression(f(x)== paste(frac(1, sqrt(2*pi* sigma^2 ) ), ” “, e^{frac(-(x-mu)^2, 2*sigma^2)})), cex=1.2)

Now the updated curve of Normal probability density function will be

greek symbols in r plot-4Example 4:

x <- dnorm( seq(-3, 3, 0.001))
> plot(seq(-3, 3, 0.001), cumsum(x)/sum(x), type=”l”, col=”blue”, xlab=”x”, main=”Normal Cumulative Distribution Function”)

The Normal Cumulative Distribution function will look like,

greek symbols in r plot-5To add formula, use text and paste command, that is

text(-1.5, 0.7, expression(phi(x)==paste(frac(1, sqrt(2*pi)), ” “, integral(e^(-t^2/2)*dt, -infinity, x))), cex=1.2)

The Curve of Normal Cumulative Distribution Function and its formula in plot will look like,

greek symbols in r plot-6

Introduction to R plot() function

Question: Can we draw graphics in R language?
Answer: Yes. R language produces high quality statistical graphs. There are many useful and sophisticated kinds of graphs available in R.

Question: Where graphics are displayed in R?
Answer: In R, all graphs are produced in a windows named Graphic Windows which can be resized.

Question: What is the use of plot function in R?
Answer: In R, plot() is a generic function that can be used to make a variety of point and line graphs. plot() function can also be used to define a coordinate space.

Question: What are the arguments of plot() function?
Answer: There are many arguments used in plot() function. Some of these arguments are x, y, type, xlab, ylab, etc. To see the full list of arguments of plot(), write the command in R console;

args(plot.default)

Question: Does all arguments are necessary to be used in R?
Answer: No. The first two arguments x and y provide the horizontal and vertical coordinates of points or lines to be plotted and also define a data-coordinate system for the graph. At least argument x is required.

Question: What is the use of the argument type in plot() function?
Answer: The argument type determines the type of the graph to be drawn. There are several types of graph that can be drawn. The default type of graph type=’p’, plots points at the coordinates specified by x and y argument. Specifying type=’l’ produces a line graph, and type=’n’ sets up the plotting region to accommodate the data set but plots nothing.

Question: Is there other types of graph are?
Answer: Yes. Setting type=’b’, draw graphs having both points and lines. Setting type=’h’ draws histogram like vertical lines and setting type=’s’ and type=’S’ draws stair-step-like lines starting horizontally and vertically respectively.

Question: What is the use of xlim and ylim in plot() function?
Answer: The argument xlim and ylim may be used to define the limits of the horizontal and vertical axes. Usually these arguments are unnecessary, because R langauge reasonably pick limits from x and y.

Question: What is the purpose of xlab and xlab argument in plot() function?
Answer: xlab and ylab argument tack character-string arguments to label the horizontal and vertical axes.

Question: Provide few examples of plot() function?
Answer: Suppose of have data set on variable x and y, such as

x <- rnorm(100, m=10, sd=10)
> y <- rnorm(100)
> plot(x, y)
> plot(x, y, xlab=’X  (Mean=10, SD=10)’,   ylab=’Y (Mean=1, SD=1)’ , type=’l’)
> plot(x, y, xlab=’X  (Mean=10, SD=10)’,   ylab=’Y (Mean=1, SD=1)’ , type=’o’)
> plot(x, y, xlab=’X  (Mean=10, SD=10)’,   ylab=’Y (Mean=1, SD=1)’ , pch=10)

 

%d bloggers like this: