Convert x-y coordinates to a data.frame for geom_path plotting on ggplot.
make_segments(xy, N=nrow(xy))
Arguments
xy: Matrix or data.frame of x-y points to plot as a path. If there are any rows that are NA-valued, the resulting path will consist of disconnected segments in those locations.
N: Number of rows of xy to plot as a path. By default, the number of rows, so all of xy.
Author(s)
Samuel Ackerman
Examples
#generate toy datasetlibrary(ggplot2)d <- as.data.frame(cbind(X=runif(50), Y=runif(50)))#create segments with some missing lines so will be some gaps)dseg <- d
dseg[ sample(20),]<-NAg <- ggplot(d, aes(x=.data$X, y=.data$Y))+ theme_bw()g <- g + stat_density2d(aes(fill=after_stat(!!str2lang("density"))), geom="tile", contour=FALSE)g <- g + scale_fill_gradient(low="white", high="black")+ theme(legend.position="bottom")g <- g + geom_path(data=make_segments(xy=dseg), aes(x=.data$X, y=.data$Y), colour="red", lwd=1.5)g