Graphical Parameters in R
Last Updated :
24 Apr, 2025
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:
Scatter plot without using any extra parameters
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:
Scatter plot with solid dots as the point
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:
Bar Chart with colored titles
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:
Bar Chart with different font sizes for the titles.
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,
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:
Adding extra axis to the plotChanging 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:
Setting the limits for the axis of the graphCustomizing 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:
Plot with colored axis labels
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:
Plot without any tick labelsChanging 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:
Using cross to plot the points on the 2D planeChanging 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:
Line Graph with dotted line
Similar Reads
Lattice Graphs in R
Lattice graphs in R are a type of graphical representation that uses a grid-like structure to display data. They are commonly used in statistics and data visualization to show the relationship between multiple variables. In R Programming Language, lattice graphs can be created using the lattice pack
6 min read
Color Palettes in R
The art of data visualization involves the use of color palettes as a key ingredient to have a uniform set of colors that will separate our data elements clearly. R guides the creation of simple and attractive charts through its color palettes. This article covers the color palettes (different types
5 min read
GGally Package in R
The GGally is an extension package to ggplot2 which contains functions to reduce the complexity of combining geoms. The plots like Scatterplot Matrix, Parallel Coordinates Plot, etc, While plotting the graphs using the ggplot() function we need to combine the geom() object to specify the type of plo
4 min read
R - Line Graphs
A line graph is a chart that is used to display information in the form of a series of data points. It utilizes points and lines to represent change over time. Line graphs are drawn by plotting different points on their X coordinates and Y coordinates, then by joining them together through a line fr
3 min read
Graph Plotting in R Programming
When it comes to interpreting the world and the enormous amount of data it is producing on a daily basis, Data Visualization becomes the most desirable way. Rather than screening huge Excel sheets, it is always better to visualize that data through charts and graphs, to gain meaningful insights. R
6 min read
Corrplot Package in R
A corrplot package is a powerful R Programming Language tool designed for intuitively and comprehensively visualizing correlation matrices. It offers a range of visualization techniques and customization options to effectively explore and communicate the relationships between variables in your data.
3 min read
Autoplot Methods in R
The autoplot() function in ggplot2 automatically generates plots for various data types, including time series, clustering results, and more. The fill aesthetic allows us to color different groups in the plot based on categorical variables, making our plots more informative and visually appealing. w
3 min read
ggpattern Package in R
The ggpattern package in R is a package for creating custom and visually appealing backgrounds for ggplot2 plots. The overall article is going to cover a comprehensive introduction to the ggpattern package in R, including its features and capabilities, installation and setup, and how to use it to cr
12 min read
Raster Data in R
In this article we will discuss what is Raster Data in R Programming Language and how we use Raster Data in different work scenarios. What is Raster Data in R?Raster data, representing spatial data in a grid format, is crucial in fields such as geography, environmental science, and remote sensing. R
3 min read
Graphical Data Analysis in R
Graphical Data Analysis (GDA) is a powerful tool that helps us to visualize and explore complex data sets. R is a popular programming language for GDA as it has a wide range of built-in functions for producing high-quality visualizations. In this article, we will explore some of the most commonly us
7 min read