geom_segment

geom_segment(mapping=NULL, data=NULL, stat="identity", position="identity", ...)

Single line segments

This page describes geom_segment, see layer and qplot for how to create a complete plot from individual components.

What do you think of the documentation? Please let me know by filling out this short online survey.

Aesthetics

The following aesthetics can be used with geom_segment. Aesthetics are mapped to variables in the data with the aes function: geom_segment(aes(x = var)). Note that you do not need quotes around the variable name.

Scales control how the variable is mapped to the aesthetic and are listed after each aesthetic.

Aesthetic Default Related scales
xrequiredcontinuous, date, datetime, discrete
yrequiredcontinuous, date, datetime, discrete
xendrequired
yendrequired
colourblackbrewer, gradient, gradient2, gradientn, grey, hue, identity, manual
size0.5identity, manual, size
linetype1identity, linetype, manual
alpha1

Layers are divided into groups by the group aesthetic. By default this is set to the interaction of all categorical variables present in the plot.

Parameters

Parameters control the appearance of the geom. In addition to the parameters listed below (if any), any aesthetic can be used as a parameter, in which case it will override any aesthetic mapping.

Returns

This function returns a layer object.

See also

Examples

> require("maps") 
>  
> xlim <- range(seals$long) 
> ylim <- range(seals$lat) 
> usamap <- data.frame(map("world", xlim = xlim, ylim = ylim, plot = 
+ FALSE)[c("x","y")]) 
> usamap <- rbind(usamap, NA, data.frame(map('state', xlim = xlim, ylim 
+ = ylim, plot = FALSE)[c("x","y")])) 
> names(usamap) <- c("long", "lat") 
>  
> p <- ggplot(seals, aes(x = long, y = lat)) 
> (p <- p + geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat), 
+ arrow=arrow(length=unit(0.1,"cm")))) 
  
> p + geom_path(data = usamap) + scale_x_continuous(limits=xlim) 
  
>  
> # You can also use geom_segment to recreate plot(type = "h") : 
> counts <- as.data.frame(table(x = rpois(100,5))) 
> counts$x <- as.numeric(as.character(counts$x)) 
> with(counts, plot(x, Freq, type = "h", lwd = 10)) 
>  
> qplot(x, Freq, data = counts, geom="segment", 
+   yend = 0, xend = x, size = I(10)) 
  
>  

What do you think of the documentation? Please let me know by filling out this short online survey.