Is there any way to create lines in R connecting two points? I am aware of lines(), function, but it creates line segment what I am looking for is an infinite length line.
abline() function in R Language is used to add one or more straight lines to a graph. The abline() function can be used to add vertical, horizontal or regression lines to plot. Syntax: abline(a=NULL, b=NULL, h=NULL, v=NULL, …)
lines() function in R Programming Language is used to add lines of different types, colors and width to an existing plot.
To draw a line plot in R, call plot() function and along with the data to plot, pass the value “l” for “type” parameter. In this tutorial, we will learn how to use plot() function to draw line plot, with example programs.
segment () function in R Language is used to draw a line segment between to particular points. x, y: coordinates to draw a line segment between provided points. Here, x0 & y0 are starting points of the line segment and x1 & y1 are ending points of line segment . Example 3: Draw multiple line segments to R Plot.
In the examples of this R tutorial, we’ll use the following example data: Our data consists of two numeric vectors x and y1. The vector x contains a sequence from 1 to 10, y1 contains some random numeric values. If we want to draw a basic line plot in R, we can use the plot function with the specification type = “l”.
Hence, data analyst or researcher try to visualize this type of graph by joining the points with lines. In ggplot2, this joining can be done by using geom_line () function.
This post explains how to draw connection lines between several locations on a map, using R. Method relies on the gcIntermediate function from the geosphere package. Instead of making straight lines, it draws the shortest routes, using great circles.
Here's an example of Martha's suggestion:
set.seed(1)
x <- runif(2)
y <- runif(2)
# function
segmentInf <- function(xs, ys){
fit <- lm(ys~xs)
abline(fit)
}
plot(x,y)
segmentInf(x,y)
#define x and y values for the two points
x <- rnorm(2)
y <- rnorm(2)
slope <- diff(y)/diff(x)
intercept <- y[1]-slope*x[1]
plot(x, y)
abline(intercept, slope, col="red")
# repeat the above as many times as you like to satisfy yourself
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With