spathxy: Reordering Points to Form a "s" Shape

Description Usage Arguments Value Examples

View source: R/spathxy.R

Description

This is a convenient function to generate points with x and y coordinates (which form a 2-column data.frame). It is much like expand.grid. The points generated by expand.grid always in this "s" order: the bottom line, form left to right, and the second line, from left to right. However, spathxy allows you choose the order you want. See examples.

Usage

1
2
3
4
5
6
7
8
spathxy(
  x,
  y,
  first = "right",
  second = "top",
  change_line = FALSE,
  stringsAsFactors = TRUE
)

Arguments

x

a vector of values to be paired with y.

y

a vector of values to be paired with x.

first

the first direction. It may be one of "right", "left", "top", "bottom". Default is "right".

second

the second direction. It may be one of "right", "left", "top", "bottom". Default is "top".

change_line

tail-to-tail or tail-to-head. Default is FALSE which means tail-to-tail. See examples.

stringsAsFactors

to be passed to data.frame.

Value

always a 3-column data frame. Column x and y are coordinates of points; column index contains the index number of points.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
library(ggplot2)
#
# dat1 is generated by expand.grid
# Note the difference between dat1 and dat2.
# dat3 is the same as dat1.
dat1=expand.grid(1: 3, 1: 7)
colnames(dat1)=c("x", "y")
dat2=spathxy(1: 3, 1: 7, 
	change_line=FALSE, first="right", second="top")
dat3=spathxy(1: 3, 1: 7, 
	change_line=TRUE, first="right", second="top")
#
mycolor=rainbow(nrow(dat1), end=0.6)
ggplot(dat1)+geom_path(aes(x, y), color=mycolor, size=3)
ggplot(dat2)+geom_path(aes(x, y), color=mycolor, size=3)
ggplot(dat3)+geom_path(aes(x, y), color=mycolor, size=3)

plothelper documentation built on July 2, 2020, 4:03 a.m.