How to add Axes to Plot in R

How to add Axes to Plot in R, In the world of data visualization, creating visually appealing and informative plots is crucial for effectively communicating insights.

The R programming language offers a plethora of tools to customize your plots, including the ability to add user-defined axis ticks using the axis() function.

In this article, we will walk you through three examples that demonstrate how to create plots with custom axis ticks in R.

Predict potential customers in R » Data Science Tutorials

Example 1: Draw Plot with Default Axis Ticks

Before diving into custom axis ticks, let’s first learn how to draw a basic plot with default axis specifications.

The plot() function in R can be used to create a scatterplot, as shown in the code snippet below:

plot(1:200)           # Default plot

This code will generate a scatterplot with default axis values.

Example 2: Plot with Specified Axis Ticks

Now, let’s move on to adding user-defined axis labels using the axis() function. First, we need to create a graph without any axis values:

plot(1:200,           # Plot without axes
     xaxt = "n",
     yaxt = "n")

Once the plot is created without axes, we can use the axis() function to add axis values.

The side parameter is used to specify which axis to modify, with 1 representing the x-axis and 2 representing the y-axis.

How to Label Outliers in Boxplots in ggplot2? (datasciencetut.com)

The c() function is used to define the tick values for the respective axis:

axis(side = 1,        # Draw x-axis
c(0, 50, 100, 150, 200)) #Just an example
axis(side = 2,        # Draw y-axis
c(10, 50, 150)) #Just an example

Example 3: Plot with Axis Mark on Top & Right Side

In some cases, you may want to add axis ticks on the top and right side of the plot.

This can be achieved using the same R code as in the previous example, but with different values for the side parameter.

Instead of 1 and 2, we will use 3 and 4 to represent the top and right axes, respectively:

plot(1:200,           # Plot without axes
     xaxt = "n",
     yaxt = "n")
axis(side = 3,        # Add axis on top
     c(0, 50, 100, 200)) #Just an example
axis(side = 4,        # Add axis on right side
     c(0, 50, 110)) #Just an example

Plotting Equations in R »

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

twenty − 16 =