Open In App

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:

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.






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.






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.




# 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.




# 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,




# 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 plot

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




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 graph

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.




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.




x <- 1:10;
y <- x*x*x
  
# plots with no tick labels
plot(x, y, xaxt="n", yaxt="n")

Output:

Plot without any tick labels

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,




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 plane

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.




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


Article Tags :