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

Import Data using read.table function

Question: How I can check my Working Directory so that I would be able to import my data in R.
Answer: To find working directory, the command getwd() can be used, that is

> getwd()

Question: How I can change working directory to my own path.
Answer: Use function setwd(), that is

> setwd(“d:/mydata”)
> setwd(“C:/Users/XYZ/Documents”)

Question: I have data set stored in text format (ASCII) that contain rectangular data. How I can read this data in tabular form. I have already set my working directory.
Answer: As data is already in a directory, which is set as working directory, use following command

> mydata <- read.table(“data.dat”)
> mydata <- read.table(“data.txt”)

mydata is named object that will have data from file “data.dat” or “data.txt” in data frame format. Each variable in data file will be named by default V1, V2, ….

Question: How this stored data can be to accessed?
Answer: To access the stored data, write data frame object name (“mydata”) with $ sign and name of the variable. That is,


Question: My data file has variables names in first row of the data file. In previous Question, variables names were V1, V2, V3, … How I can get actual names of the variable store in first row of data.dat file.
Answer: Instead of reading data file with default values of arguments, use

> read.table(“data.dat”, header = TRUE)

Question: I want to read a data file which is not store in working directory?
Answer: To access the data file which is not stored in working directory, provide complete path of the file, such as.

> read.table(“d:/data.dat” , header = TRUE)
> read.table(“d:/Rdata/data.txt” , header = TRUE)

Note that read.table() is used to read the data from external files that has a normally a special form:

  • The first line of the file should have a name for each variable in the data frame. However, if first row does not contains name of variable then header argument should not be set to FALSE.
  • Each additional line of the file has it first item a row label and the values for each variable.

In R it is strongly suggested that variables need to be held in data frame. For this purpose read.table() function can be used. For further details about read.table() function use,



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;


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


R Basics

Question 1: How can I retrieve (load) the work that is saved using history function in R?
Answer: The loadhistory() function will load an “.Rhistory“file.

> loadhistory(“d:/file_name.Rhistory”)

This function will load file named “file_name.Rhistory” from D: drive.

The other way may be to access .Rhistory file through the file menu. For this click File and then Load history. From the dialog box appeared  browse to the folder where you saved the .Rhistory file and click open to start working.

Question 2: How do I use a script of commands and functions saved in a text file?
Answer: The script of commands and functions saved in a text file (also called script file) can be used by writing the following command.

> source(“d:/file_name.txt”)

The “file_name.txt” will load from D: drive.

Question 3: How do I get R to echo back the R commands and functions in a script file that I am sourcing into R? That is, the functions that I have written, I want to see these functions are being executed.
Answer: use echo=TRUE argument by using source() function

source(“d:/file_name.txt”, echo=T)

Question 4: How do I close the help file when working on a Macintosh operating system?
Answer: Typing just q will close the help file and bring you back to the R console.

Question 5: How can I see a list of currently available objects in R?
Answer: Use the objects() or ls() functions to see the list of objects currently available

> ls()

Question 6: How do I remove/delete unwanted objects and functions?
Answer: The rm() function can be used to delete or remove the objects that are not required. Commands  below will delete objects named object_name1 & object_name2 and functions named function_name1 & function_name2.

> rm(object_name1, object_names2)
> rm(function_name1, function_name2)

R FAQs: Getting Help in R

Question: How one can get help about different command in R Language?
Answer: There are many ways to get help about different command (functions). R has built-in help facility which is similar to man facility in Unix. For beginners of R language, help() function or ? can be used to get help about different commands of R language.

Questions: Provide some examples about getting help?
Answer: To get more information on any specific R command (function), for example for getting help about solve(), lm(), plot() etc, write the following commands at R prompt:

> help(solve)
> help(lm)
> help(plot)

Question: Can one get help for special symbols, characters in R Language?
Answer: Yes one can get help for special characters. For example;

> help(“[[“)
> help(“[“)
> help(“^”)
> help(“$”)
> help(“%%”)

Question: What help.start() does?
Answer: The help.start() will launch a web browser that allows the help pages to browsed with hyperlinks. It can be a better way to get help about different functions.

Question: There is command. What for purpose it is?
Answer: The command allows searching for help in various ways. To get what functions does, write this command at R prompt;

> help(

Question: Provide some details about function and also illustrate it by providing some examples?
Answer: The allows for searching the help system for documentation matching a given character string in the (file) name, alias, title, concept or keyword entries (or any combination thereof), using either fuzzy matching or regular expression matching. Names and titles of the matched help entries are displayed nicely formatted. The examples are:

>“linear models”)

Question: How ? can be used to get help in R language?
Answer: The ? mark can be used to get help in Windows version of R Language. For example;

> ?print
> ?help
> ?”[[“
> ?methods
> ?lm

For further detials, follow the link Getting Help in R Language