# The segments R Function | 3 Example Codes

**Basic R Syntax:**

segments(x0 = 1, y0 = 1, x1 = 2, y1 = 2) |

segments(x0 = 1, y0 = 1, x1 = 2, y1 = 2)

The segments R function **draws a line segment between two pairs of points**. The basic syntax for segments in R is shown above.

In the following article, I’m going to show you **three examples** for the application of the segments command in the R programming language.

So if you want to know more about the segments function, keep reading…

## Example 1: Basic Application of the segments R Function

The basic usage of the segments R function is quite straight forward. Let’s first create an empty plot, to which we can add a line segment:

plot(0, 0, col = "white", xlab = "", ylab = "") # Create empty example plot |

plot(0, 0, col = "white", xlab = "", ylab = "") # Create empty example plot

**Graphic 1: Empty Plot as Preparation for Segments R Example.**

Now, let’s apply the segments R function in order to draw a line segment:

segments(x0 = - 1, y0 = - 0.5, x1 = 0.5, y1 = 0) # Draw one line |

segments(x0 = - 1, y0 = - 0.5, x1 = 0.5, y1 = 0) # Draw one line

**Graphic 2: Basic Application of segments() Function.**

As you can see, we had to specify four different values within the segments function:

- x0 & y0: The x-y coordinates of the starting point from which to draw the line segment.
- x1 & y1: The x-y coordinates of the ending point to which to draw the line segment.

That’s it!

However, the segments R function is very flexible. In the next example I’m going to show you some ways to make the line segments of your graphic much prettier…

## Example 2: How to Modify Color, Thickness & Line Type

In the following example, I’m going to show you how to modify color, thickness and line type of your line segments. First, we need to create an empty plot (as in Example 1):

plot(0, 0, col = "white", xlab = "", ylab = "") # Create empty example plot |

plot(0, 0, col = "white", xlab = "", ylab = "") # Create empty example plot

We then can apply the segments command as in Example 1, but with additional specifications:

segments(x0 = - 1, y0 = - 0.5, x1 = 0.5, y1 = 0, # Draw one line as in Example 1 col = "cornflowerblue", # Color of line lwd = 5, # Thickness of line lty = "dotted") # Line type |

segments(x0 = - 1, y0 = - 0.5, x1 = 0.5, y1 = 0, # Draw one line as in Example 1 col = "cornflowerblue", # Color of line lwd = 5, # Thickness of line lty = "dotted") # Line type

**Graphic 3: Manual modifications of segments() Function: Color, Thickness & Line Type.**

As you can see, I changed the color, the thickness, and the line type of our line segment. However, many other arguments for modifying line segments can be found in the par help documentation.

## Example 3: Add Multiple Line Segments to R Plot

The segments R function can also be useful, when you need to draw multiple lines to your graph. Again, let’s start with an empty figure:

plot(0, 0, col = "white", xlab = "", ylab = "") # Create empty example plot |

plot(0, 0, col = "white", xlab = "", ylab = "") # Create empty example plot

Now, let’s crate a data frame, in which we specify multiple starting and ending x-y coordinates:

mult_seg <- data.frame(x0 = c(0.7, 0.2, - 0.9, 0.5, - 0.2), # Create data frame with line-values y0 = c(0.5, 0.5, 0.6, - 0.3, 0.4), x1 = c(0, 0.2, 0.4, - 0.3, - 0.6), y1 = c(- 0.1, 0.3, - 0.6, - 0.8, 0.9)) |

mult_seg <- data.frame(x0 = c(0.7, 0.2, - 0.9, 0.5, - 0.2), # Create data frame with line-values y0 = c(0.5, 0.5, 0.6, - 0.3, 0.4), x1 = c(0, 0.2, 0.4, - 0.3, - 0.6), y1 = c(- 0.1, 0.3, - 0.6, - 0.8, 0.9))

As you can see, our data matrix consists of four columns:

- Column 1: x0 coordinates
- Column 2: y0 coordinates
- Column 3: x1 coordinates
- Column 4: y1 coordinates

After creating this data frame, we can draw multiple line segments with the following R code:

segments(x0 = multiple_segments$x0, # Draw multiple lines y0 = multiple_segments$y0, x1 = multiple_segments$x1, y1 = multiple_segments$y1) |

segments(x0 = multiple_segments$x0, # Draw multiple lines y0 = multiple_segments$y0, x1 = multiple_segments$x1, y1 = multiple_segments$y1)

**Graphic 4: How to Draw Multiple Line Segments.**

With this procedure, you can draw as many line segments as you want with one simple application of the segments command.

But there is even more…

## Video Tutorial: R Boxplot with Segments

So far, I have shown you how to draw a line segment to an empty plot. However, it gets even more interesting, when you use the segments function to visualize real statistical results. You can learn more about that in the following video of the YouTube channel DWR447. The video explains how to use the segments function in combination with boxplots.

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