Plot Function in R

This article about the plot function in R Language gives some introduction about the plot() function, the use and purpose of its arguments, and a few examples are provided. Using the R plot function one can draw different graphical representations and the arguments of the plot() function can be used to enhance the graph.

Introduction to Graphics in R Language

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 window named Graphic Windows which can be resized.

Question: What is the use of the 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.

Important Arguments of the Plot Function in R

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

args(plot.default)

Question: Are all arguments 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 define a data-coordinate system for the graph. At least argument x is required. Note that many of the arguments are set to default values in the plot function.

Question: What is the use of the argument type in the plot() function?
Answer: In the R plot function, the argument type determines the type of the graph to be drawn. Several types of graphs can be drawn. The default type of graph type=’p’, plots points at the coordinates specified by the 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.

Other Types of Graphs: Setting type Argument

Question: Are there other types of graphs?
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 arguments xlim and ylim may be used to define the limits of the horizontal and vertical axes. Usually, these arguments are unnecessary, because R language reasonably picks limits from x and y.

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

Examples of R Plot Function in R

Question: Provide a few examples of the R plot function.
Answer: The following are a few examples of R plot functions. Suppose you have a data set on variables 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)
Introduction to plot function in R

https://gmstat.com

https://itfeature.com

Objects in R Language: Secrets

The post is about objects in R Language. In R everything is an object. The commands run in a session can be saved or loaded in a file as history.

Loading Saved Work

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

loadhistory("d:/file_name.Rhistory")

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

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

Script File in R

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 a script file) can be used to write 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.

Objects in R Language: Currently Available Objects

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

objects()
ls()
Objects in R Language

Remove Objects and Functions

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

https://gmstat.com

https://itfeature.com

Getting Help with R

Getting help with R is important to learning the language and getting expertise in R.

Question: How one can get help with different commands in the R Language?
Answer: There are many ways to get help with the different commands (functions). There is a built-in help facility which is similar to the man facility in Unix. For beginners to get help, the help() function or the symbol ? (question mark only) can be used to get help with different commands.

Help Function in R

Questions: Provide some examples of getting help with different functions used in R.
Answer: To get more information on any specific command (function), for example for getting help with solve( ), lm( ), plot( ), etc., write the following commands at the prompt:

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

Question: Can one get help for special symbols, and 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 be browsed with hyperlinks. It can be a better way to get help with different functions.

help.search Function

Question: There is help.search( ) command. For what purpose it is?
Answer: The help.search() command allows searching for help in various ways. To get what help.search( ) functions do, write this command at the prompt;

help(help.search)

Question: Provide some details about help.search( ) function and also illustrate it by providing some examples.
Answer: The help.search( ) 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:

help.search("linear")
help.search("linear models")
help.search("print")
help.search("cat")
Getting Help with R Language help.search("linear")

? Operator

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

?print
?help
?"[["
?methods
?lm
Getting Help in R: Frequently Asked Questions About R

MCQs in Statistics

MCQs General Knowledge