R Plots Par

broken image


It is fairly straightforward to set the margins of a graph in R by calling the par function with the mar (for margin!) argument.

R Plot Function


plot() is a generic X Y plotting function.


x,y:Vector of coordinates
main, sub: an overall or sub title for the plot
xlab, ylab: a title for the x or y axis
xlim, ylim: set the limits of the x or y axis
col: color of the plot, see color for color's chart
asp: the y/x aspect ratio
type: character vector consisting of one or more of the following: 'p' (points), 'l' (line), 'b' (both), 'h' (histogram),'c' (for the lines part alone of b), 'o' (both overplotted), 's' (stair steps), 'S' (other steps), 'n' (no plotting).
First let's make a simple plot:


To save the image as a file, Plots panel->File->Save as.
The par(...) controls the general layout of the plot. For example,par(mar = c(5, 4, 2, 1)) defines the bottom margin as 5, left margin 4,top margin 2 and right margin as 1.
The default type is a point plot (type='p'). The possible types include:

There are 2 margin areas in base R plots: margin and oma.You can control their size calling the par function before your plot and giving the corresponding arguments. Mar for margin. Oma for outer margin area. For both arguments, you must give four values giving the desired space in the bottom, left, top and right part of the chart respectively. Browse other questions tagged r plot or ask your own question. The Overflow Blog The Loop: Our Community & Public Platform strategy & roadmap for Q1 2021. Podcast 308: What are the young developers into? Everyone's getting AWS Featured on Meta. The R plot function allows you to create a plot passing two vectors (of the same length), a dataframe, matrix or even other objects, depending on its class or the input type. We are going to simulate two random normal variables called x and y and use them in almost all the plot examples. The default values of the three plotting parameters are shown using character strings of their argument names in par (see par (char)), followed by sample code creating four plots, where one additional plotting parameter is modified in each plot.

Description
Points, default
Lines
Points with line connection
Line connections without points
Both overplotted
Histogram like vertical lines
Stair steps
Par mar in r
Stair steps, another style
No plotting

Let's use less points and plot with line connections. We willuse blue colored line and points, and with axis labels both to X and Y axis as well as a main title of the plot:
R plot par options


Add more data to the plot:


If we want to move the legend out of the main plot area, we need some more work.First use layout(...) function to define 2 plots on one layer side by side,and then we plot the same data on both plots, with the plot on the right side in white color,thus invisible (just providing the scale), and finally we plot the legend on the second plot.


Function curve() can plot equations like y = ax^2 + bx + c.


Some low-level plotting functions include points,abline,text,mtext,segments,axis etc.

Legend function in R adds legend box to the plot. legend() function in R makes graph easier to read and interpret in better way. lets see an example on how to add legend to a plot with legend() function in R.

Syntax of Legend function in R:

legend(x, y = NULL, legend, fill = NULL, col = par('col'),border = 'black', lty, lwd, pch)
x, ythe x and y co-ordinates which is used to position the legend.
legenda character vector of legend names
fillfill legend box with the specified colors.
colthe color of points or lines appearing in the legend.
borderthe border color for the legend box
lty, lwdthe line types and widths for lines appearing in the legend.
pchthe plotting symbols appearing in the legend

Example of Legend function in R:

Let's depict how to create legend in R with an example. Before that lets create basic scatter plot using plot() function with red colored rounded dots as shown below.

output will be

Now, lets again add an another sets of scatter plot with point function with blue color pyramids as shown below.

So the resultant chart will be

Add legend to the top left corner of the plot with legend function in R:

Now let's add the legend to the above scatter plot with legend function in R, to make it more readable

In the above function we have added legend to the top left corner of the graph at co-ordinates x= -3 and y=7 so the output will be

Add legend to the top right corner of the plot with legend function in R:

R Plots Par

In the above function we have added legend to the top right corner of the graph at co-ordinates x= 4 and y=7 so the output will be

Specify legend position by keywords

R Plots Par

The position of the legend can be specified also using the following keywords : 'bottomright', 'bottom', 'bottomleft', 'left', 'topleft', 'top', 'topright', 'right' and 'center'.

The effect of using each of these keywords are shown in the figure below :

Add legend Box and background color to legend box:

A Green color box is added to the legend using box.col='green'. and the background of this box is filled with light blue using bg= 'lightblue' as shown below.

so the resultant plot will have green color box, with light blue back ground

R Plot Par

Plots
Stair steps, another style
No plotting

Let's use less points and plot with line connections. We willuse blue colored line and points, and with axis labels both to X and Y axis as well as a main title of the plot:


Add more data to the plot:


If we want to move the legend out of the main plot area, we need some more work.First use layout(...) function to define 2 plots on one layer side by side,and then we plot the same data on both plots, with the plot on the right side in white color,thus invisible (just providing the scale), and finally we plot the legend on the second plot.


Function curve() can plot equations like y = ax^2 + bx + c.


Some low-level plotting functions include points,abline,text,mtext,segments,axis etc.

Legend function in R adds legend box to the plot. legend() function in R makes graph easier to read and interpret in better way. lets see an example on how to add legend to a plot with legend() function in R.

Syntax of Legend function in R:

legend(x, y = NULL, legend, fill = NULL, col = par('col'),border = 'black', lty, lwd, pch)
x, ythe x and y co-ordinates which is used to position the legend.
legenda character vector of legend names
fillfill legend box with the specified colors.
colthe color of points or lines appearing in the legend.
borderthe border color for the legend box
lty, lwdthe line types and widths for lines appearing in the legend.
pchthe plotting symbols appearing in the legend

Example of Legend function in R:

Let's depict how to create legend in R with an example. Before that lets create basic scatter plot using plot() function with red colored rounded dots as shown below.

output will be

Now, lets again add an another sets of scatter plot with point function with blue color pyramids as shown below.

So the resultant chart will be

Add legend to the top left corner of the plot with legend function in R:

Now let's add the legend to the above scatter plot with legend function in R, to make it more readable

In the above function we have added legend to the top left corner of the graph at co-ordinates x= -3 and y=7 so the output will be

Add legend to the top right corner of the plot with legend function in R:

In the above function we have added legend to the top right corner of the graph at co-ordinates x= 4 and y=7 so the output will be

Specify legend position by keywords

The position of the legend can be specified also using the following keywords : 'bottomright', 'bottom', 'bottomleft', 'left', 'topleft', 'top', 'topright', 'right' and 'center'.

The effect of using each of these keywords are shown in the figure below :

Add legend Box and background color to legend box:

A Green color box is added to the legend using box.col='green'. and the background of this box is filled with light blue using bg= 'lightblue' as shown below.

so the resultant plot will have green color box, with light blue back ground

R Plot Par

Add Title and font to the legend

Title is added to the legend using Title keyword. Font of that title can be assigned with text.font keyword.

title: The title of the legend
text.font: an integer specifying the font style of the legend text; possible values are :
1: normal
2: bold
3: italic
4: bold and italic

so the resultant plot will have green color box, with light blue back ground, With Bold italic title as legend on the top left corner is shown below

R Plot Parametric Curve

for further details kindly refer here





broken image