ggplot2::geom_segment()
   get_help()
docs
The geom_segment()
function is part of the {ggplot2}
package, which is part of the {tidyverse}
.
The geom_segment()
function is used within {ggplot2}
plots to create line segments. Line segments have x and y start and end coordinates.
x
: The x-coordinate for the line segment to start atxend
: The x-coordinate for the line segment to end aty
: The y-coordinate for the line segment to start atyend
: The y-coordinate for the line segment to end atcolor
(colour
): The color of the line segmentsize
: The width of the line segmentlinetype
: The style of the line segment, where options include "solid"
, "dashed"
, "dotted"
, "dotdash"
, "longdash"
, "twodash"
. The default is "solid"
.To use this function, you need to either first load the {ggplot2}
library, or always use the function with ggplot2::geom_segment()
notation.
# Load the library
library(ggplot2)
# Or, load the full tidyverse:
library(tidyverse)
# Or, use :: notation
::geom_segment() ggplot2
Some of the examples below use the msleep
and carnivores
datasets. Learn more about this dataset with get_help("msleep")
and get_help("carnivores")
.
# Show the msleep dataset with head()
head(msleep)
## # A tibble: 6 × 11
## name genus vore order conservation sleep_total sleep_rem sleep_cycle awake brainwt bodywt
## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Owl … Aotus omni Prim… <NA> 17 1.8 NA 7 0.0155 0.48
## 2 Moun… Aplo… herbi Rode… nt 14.4 2.4 NA 9.6 NA 1.35
## 3 Grea… Blar… omni Sori… lc 14.9 2.3 0.133 9.1 0.00029 0.019
## 4 Cow Bos herbi Arti… domesticated 4 0.7 0.667 20 0.423 600
## 5 Thre… Brad… herbi Pilo… <NA> 14.4 2.2 0.767 9.6 NA 3.85
## 6 Nort… Call… carni Carn… vu 8.7 1.4 0.383 15.3 NA 20.5
# Show the carnivores dataset
carnivores
## # A tibble: 9 × 4
## name genus awake brainwt
## <chr> <fct> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445
## 2 Cheetah Acinonyx 11.9 NA
## 3 Dog Canis 13.9 0.07
## 4 Gray seal Haliochoerus 17.8 0.325
## 5 Jaguar Panthera 13.6 0.157
## 6 Lion Panthera 10.5 NA
## 7 Northern fur seal Callorhinus 15.3 NA
## 8 Red fox Vulpes 14.2 0.0504
## 9 Tiger Panthera 8.2 NA
# Create a tibble with tibble::tribble() with values for the line segment we want to draw, and draw it!
<- tibble::tribble(
example_tibble ~x_var, ~xend_var, ~y_var, ~yend_var,
0, 3.5, 2, 4
)
# Look at tibble:
example_tibble
## # A tibble: 1 × 4
## x_var xend_var y_var yend_var
## <dbl> <dbl> <dbl> <dbl>
## 1 0 3.5 2 4
ggplot(example_tibble) +
aes(x = x_var,
xend = xend_var,
y = y_var,
yend = yend_var) +
geom_segment()
# Scatterplot of awake against sleep_rem, where we add a line segment in at specified coordinates
ggplot(msleep) +
aes(x = sleep_rem, y = awake) +
geom_point() +
# Add a segment:
geom_segment(x = 0, xend = 2, y = 7, yend = 9, color = "red")
# Scatterplot of awake against sleep_rem, where we add a line segment in at specified coordinates
# This example uses a different linetype
ggplot(msleep) +
aes(x = sleep_rem, y = awake) +
geom_point() +
# Add a segment:
geom_segment(x = 0, xend = 2, y = 7, yend = 9, color = "red", linetype = "dashed")
# Plot the literal values of `awake` for each name in the carnivores dataset, as a lollipop plot
ggplot(carnivores) +
aes(x = name, y = awake) +
geom_point() +
# To understand this, think carefully about the grammar and how data maps to these aesthetics
geom_segment(aes(x = name, # For a vertical line, x starts and stops at same coordinate.
xend = name,
y = 0, # y starts from 0
yend = awake)) # and goes to the awake value