How to add Axes to Plot in R

[This article was first published on R Archives » Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to add Axes to Plot in R appeared first on Data Science Tutorials

Unravel the Future: Dive Deep into the World of Data Science Today! Data Science Tutorials.

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 »

The post How to add Axes to Plot in R appeared first on Data Science Tutorials

Unlock Your Inner Data Genius: Explore, Learn, and Transform with Our Data Science Haven! Data Science Tutorials.

To leave a comment for the author, please follow the link and comment on their blog: R Archives » Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)