Graphical Parameters in R
In R programming language we can customize the appearance of plots. There are a variety of graphical parameters that can be used to do so. Some of the most commonly used graphical parameters include:
- xlim and ylim: These parameters set the limits of the x and y axes, respectively. They take a vector of two values: the minimum and maximum values of the axis.
- xlab and ylab: These parameters set the labels of the x and y axes, respectively. They take a string value.
- main: This parameter sets the main title of the plot. It takes a string value.
- col: This parameter sets the color of points or lines in a plot. It takes a string value specifying a color name or a code in the format “#RRGGBB”, where RR, GG, and BB are the red, green, and blue components of the color, respectively.
- lwd: This parameter sets the width of lines in a plot. It takes numeric value.
- sub: This parameter sets the sub-title/label of the plot.
- pch: This parameter sets the plotting character for points in a plot. It takes an integer value between 0 and 25.
- lty: This parameter can be used to change the line types of the plot
- font: This parameter sets the font style and font size in the plot. We can make the text bold, italic, bold italic, etc.
- cex: It is a short form for character expansion. This parameter sets the size of elements in the plot, such as points or text. cex takes a numeric value with, 1 being the default size.
Similarly, there are many more graphical parameters that could be used to customize the appearance of the plot. We can set this parameter by giving them directly to the plotting function. This can be understood by the following example. Let’s plot a graph with random points with no parameters.
R
x <- rnorm (100) y <- rnorm (100) plot (x, y) |
Output:
This is the output we obtain, let’s try to customize this plot.
R
x <- rnorm (100) y <- rnorm (100) plot (x, y, xlim = c (-2, 2), ylim = c (-2, 2), xlab = "X" , ylab = "Y" , main = "My Plot" , col = "blue" , pch = 16, lwd= 2) |
Output:
We can see that this plot is more informative than the previous one. We have added a title and labeled the y and x-axis, also given the limits of the axes (-2 to 2), the color is also changed from black to blue. Let’s try some examples using bar graphs.
R
# plotting a bar-graph with title and color barplot ( c (1,3), main= "Main title" , xlab= "X axis title" , ylab= "Y axis title" , col.main= "red" , col.lab= "blue" ) |
Output:
Now let’s change the size font size of the axis titles and the main title of the graph.
R
# Increasing the size of titles barplot ( c (1,3), main= "Main title" , xlab= "X axis title" , ylab= "Y axis title" , cex.main=2, cex.lab=1.7) |
Output:
Let us see some more examples for a better understanding.
Adding an Axis to a Plot
We add an axis to our plot using the axis() function in R programming. It has an attribute named ‘side’, which takes an integer as a value. These values are,
- below
- left
- above
- right
R
# This will create a plot of x # and y with no axis, x <- c (1, 2, 3, 4, 5) y <- x^2 plot (x, y, xaxt = "n" , yaxt = "n" ) # then add x and y axis to the plot. axis (1) axis (2) |
Output:
Changing Axis Scale
We can set x and y-axis limits by specifying the minimum and maximum values of each axis. The below code will create a plot of x and y with x-axis limits between 0 and 15 and y-axis limits between 1 and 100, and also with a log scale on the x and y-axis
R
x <- c (1, 2, 3, 4, 5) y <- x^2 # setting the limits for axis plot (x, y, xlim= c (1,15), ylim= c (1,100), log= "xy" ) |
Output:
Customizing Tick Mark Labels
We can the appearance of the tick marks in the plot. We can change the color, font-size of tick labels using the col.axis function.
R
x <- 1:10 y <- x*x*x # adding red color to the tick labels plot (x,y, col.axis= "red" ) |
Output:
We can also hide the tick labels using xaxt and yaxt parameters. They both take a single character value, “s” for showing the axis and “n” for hiding the axis.
R
x <- 1:10; y <- x*x*x # plots with no tick labels plot (x, y, xaxt= "n" , yaxt= "n" ) |
Output:
Changing plotting symbols
We have many plotting symbols in R. These are generated using the ‘pch’ parameter which takes up an integer value to set the type of symbol. For example,
- pch = 0, for square
- pch = 1, for circle
- pch = 2, for triangle point up
- pch = 3, for plus
- pch = 4, for cross
- pch = 5, for diamond , etc.
R
x <- c (1, 2, 3, 4, 5) y <- x^2 # makes the pointer a cross plot (x, y, pch = 4, col = "blue" ) |
Output:
Changing the Line Types
In R, we can change the line types of a plot using the lty parameter. It can be solid, blank, dotted, dashed, etc. Ity takes up an integer value which is 0 for blank, 1 for solid, 3 for dotted, and so on.
R
x <- c (1, 2, 3, 4, 5) y <- x^2 # makes a line graph with dotted lines plot (x, y, type = "l" , lty = 2) |
Output:
Contact Us