# R pretty Function | 3 Example Codes (Interval Sequence & Set Axis Labels of Plot)

**Basic R Syntax:**

pretty(x) |

pretty(x)

The pretty R function **computes a sequence of equally spaced round values**. The basic R syntax for the pretty command is illustrated above.

In this R tutorial, I’m going to show you **three examples** for the application of pretty in the R programming language.

So without further ado, let’s get started…

## Example 1: Default Application of pretty()

The R pretty function is quite easy to apply. I’ll show you the default usage of pretty() based on a simplified example.

Let’s first create an example vector:

x <- 1:100 # Create example vector |

x <- 1:100 # Create example vector

Our example vector contains all integer numbers from 1 to 100.

Now, let’s apply pretty with the default options to this integer vector:

pretty(x) # Apply pretty function to vector # 0 20 40 60 80 100 |

pretty(x) # Apply pretty function to vector # 0 20 40 60 80 100

As you can see, pretty() converted our vector to five intervals of equal length.

That was easy. However, you can specify additional options within the pretty function…

## Example 2: Specify n-option Within pretty in R

One of the options that pretty provides is the n-option, which specifies the desired number of intervals.

By default, this option is set to *n = 5* (as you have seen in Example 1). However, we can modify this default specification with the following line of code:

pretty(x, n = 10) # Apply pretty with modified intervals # 0 10 20 30 40 50 60 70 80 90 100 |

pretty(x, n = 10) # Apply pretty with modified intervals # 0 10 20 30 40 50 60 70 80 90 100

We set *n = 10*, leading to an interval vector consisting of ten intervals.

So, how can we use this in practice?

## Example 3: Plot With Pretty Axis Labels

The R pretty function is quite often used to modify the axis of base plots. Let’s create a second example vector…

y <- 1:100 # Create second example vector |

y <- 1:100 # Create second example vector

…and the let’s plot our data in a scatterplot:

plot(x, y, # Create plot of two vectors main = "Default Axis Labels") # Default axis labels |

plot(x, y, # Create plot of two vectors main = "Default Axis Labels") # Default axis labels

**Figure 1: R Plot With Default Axis Labels.**

As you can see, our two vectors x and y both consist of the integer numbers from 1 to 100. When we plot these data, our x- and y-axes have the labels 0, 20, 40, 60, 80, and 100 (i.e. five intervals).

Let’s assume that we want like to modify the number of intervals. Then, we first need to create a graphic without any axes:

plot(x, y, # Create plot of two vectors xaxt = "n", yaxt = "n", # Delete axis labels main = "Pretty Axis Labels") |

plot(x, y, # Create plot of two vectors xaxt = "n", yaxt = "n", # Delete axis labels main = "Pretty Axis Labels")

**Figure 2: R Plot Without Default Axis Labels.**

And then we can use the axis() function in combination with pretty() in order to change the number of intervals:

axis(side = 1, pretty(x, n = 20)) # Add modified x-axis labels via pretty() axis(side = 2, pretty(y, n = 3)) # Add modified y-axis labels via pretty() |

axis(side = 1, pretty(x, n = 20)) # Add modified x-axis labels via pretty() axis(side = 2, pretty(y, n = 3)) # Add modified y-axis labels via pretty()

**Figure 3: R Plot With Modified Axis Labels Via pretty() Function.**

As you can see, I have changed the desired number of intervals to 20 for the x-axis and to 3 for the y-axis.

If you want, you can change many other options within the pretty R function as well. Just type *?pretty* to your RStudio console to print an overview of all available options (e.g. min.n, shrink.sml, high.u.bias, u5.bias, eps.correct…).

## Further Options to Customize R Plots

Of cause, there are many other possibilities how you can modify the axes of your R graphs. You can find a basic overview in the following video of the DataCamp YouTube channel:

## Further Reading

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R