R Tutorial

Fundamentals of R

Variables

Input and Output

Decision Making

Control Flow

Functions

Strings

Vectors

Lists

Arrays

Matrices

Factors

DataFrames

Object Oriented Programming

Error Handling

File Handling

Packages in R

Data Interfaces

Data Visualization

Statistics

Machine Learning with R

Adding axis to a Plot in R

In R, the base graphics system provides a straightforward way to produce plots and customize them. One of the common customizations is adding and adjusting the axes. In this tutorial, I'll guide you through adding and customizing axes to a basic scatter plot using R's base graphics.

1. Creating a Basic Scatter Plot:

We'll start by creating a basic scatter plot using plot():

# Sample data
x <- c(1, 2, 3, 4, 5)
y <- c(1.1, 2.1, 3.1, 4.2, 5)

# Basic scatter plot
plot(x, y, xlab="", ylab="", xaxt="n", yaxt="n")

The parameters xlab and ylab are set to empty to remove axis labels initially. The xaxt="n" and yaxt="n" options remove the x and y axes respectively.

2. Adding Custom Axes:

Now, we will add custom axes to the plot:

# Add x-axis at the bottom
axis(side=1, at=seq(1, 5, 1))

# Add y-axis on the left
axis(side=2, at=seq(1, 6, 1))

The side parameter determines the side on which the axis is drawn:

  • 1 = bottom
  • 2 = left
  • 3 = top
  • 4 = right

The at parameter specifies the locations at which tick marks are to be drawn.

3. Customizing Axes:

3.1. Custom Labels:

You can provide custom labels to the axis ticks using the labels parameter:

axis(side=1, at=seq(1, 5, 1), labels=c("one", "two", "three", "four", "five"))

3.2. Change Axis Line and Tick Marks:

To modify the appearance of the axis line and tick marks, you can use the lty, lwd, and tck parameters:

axis(side=2, at=seq(1, 6, 1), lty=2, lwd=2, tck=-0.02)

Here, lty is the line type, lwd is the line width, and tck is the length of the tick mark (negative value indicates the tick marks are drawn inside the plot area).

3.3. Adding Axis Labels:

mtext(text="X-Axis Label", side=1, line=3)
mtext(text="Y-Axis Label", side=2, line=3)

The line parameter indicates the number of lines away from the plot at which the text should be written.

4. Finishing the Plot:

You can add other elements, such as a title or grid lines, to finish off your plot:

title("Scatter Plot with Custom Axes")
grid()

This tutorial gives you a basic overview of how to add and customize axes in R's base graphics system. Remember, there are other packages like ggplot2 that provide even more flexible and powerful plotting capabilities.

  1. R plot add axis example: Add custom axes to a plot in R:

    # R plot add axis example
    x <- c(1, 2, 3, 4, 5)
    y <- c(2, 4, 6, 8, 10)
    plot(x, y, type = "l", col = "blue", xlab = "X-axis", ylab = "Y-axis")
    
  2. Add x-axis and y-axis labels in R plot: Label the x-axis and y-axis for better clarity:

    # Adding axis labels
    x <- c(1, 2, 3, 4, 5)
    y <- c(2, 4, 6, 8, 10)
    plot(x, y, type = "l", col = "blue", xlab = "Time", ylab = "Value")
    
  3. Change axis ticks in R plot: Modify axis ticks to control the intervals:

    # Changing axis ticks
    plot(x, y, type = "l", col = "blue", xaxt = "n")
    axis(1, at = c(1, 3, 5), labels = c("Jan", "Mar", "May"))
    
  4. Adjusting axis limits in R plot: Set custom axis limits for a specific range:

    # Adjusting axis limits
    plot(x, y, type = "l", col = "blue", ylim = c(0, 12))
    
  5. Rotating axis labels in R plot: Rotate axis labels for better readability:

    # Rotating axis labels
    plot(x, y, type = "l", col = "blue", xlab = "Time", ylab = "Value")
    par(las = 2)  # Rotate x-axis labels
    
  6. Multiple axes in a single R plot: Display multiple axes in a single plot:

    # Multiple axes
    x <- seq(0, 2 * pi, length.out = 100)
    y1 <- sin(x)
    y2 <- cos(x)
    par(mar = c(5, 4, 4, 5) + 0.1)
    plot(x, y1, type = "l", col = "blue", xlab = "Angle", ylab = "sin(x)")
    par(new = TRUE)
    plot(x, y2, type = "l", col = "red", xaxt = "n", yaxt = "n", xlab = "", ylab = "")
    
  7. Hide axis in R plot: Hide one or both axes in an R plot:

    # Hide axis
    plot(x, y, type = "l", col = "blue", xaxt = "n", yaxt = "n", xlab = "", ylab = "")
    
  8. Logarithmic axis in R plot: Use a logarithmic scale for one or both axes:

    # Logarithmic axis
    x <- c(1, 10, 100, 1000)
    y <- c(2, 4, 6, 8)
    plot(x, y, type = "l", col = "blue", log = "x")
    
  9. Secondary axis in R plot: Add a secondary axis to the plot:

    # Secondary axis
    x <- seq(0, 10, by = 1)
    y1 <- x^2
    y2 <- x^3
    plot(x, y1, type = "l", col = "blue", xlab = "X", ylab = "Y1")
    par(new = TRUE)
    plot(x, y2, type = "l", col = "red", xlab = "", ylab = "", axes = FALSE)
    axis(side = 4)