Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: draw a line between two points in ggplot

Tags:

r

ggplot2

I have a data frame looks like this one:

X      Y
100    50
80     60
70     90
110    60
30     20
... ...

Around 100 lines more. Both X column and Y column are numeric

When I plot this points, I would like to draw a line between first point (100,50) and any of the rest points. In other words, I would like to have a line connects (100,50) with (80,60), a line connects (100,50) with (70,90), a line connects (100,50) with (110,60) but there is no line between (80,60) and (70,90). All lines start from the first point.

I do not have the third column. I can not use group. I wonder whether I can still plot this graph in the ggplot.

Thank you

like image 294
cutebunny Avatar asked Dec 17 '15 20:12

cutebunny


People also ask

How do you draw a line between two points in R?

segment() function in R Language is used to draw a line segment between to particular points. Parameters: 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 .

How do I join up points in ggplot2?

In ggplot2 we can add lines connecting two data points using geom_line() function and specifying which data points to connect inside aes() using group argument.

What is %>% in Ggplot?

the %>% is a pipe operator that is actually part of the dplyr library (along with the filter function) not from the ggplot2 library. To sample 1%, there is a sample_frac function in the dplyr library. It would be something like (df %>% sample_frac(0.01))


1 Answers

The idea is to use group. For this, you have to add a third column that can be used for grouping. A possible way to achieve this is as follows.

First, I define the sample data

df <- read.table(text = "X      Y
      100    50
      80     60
      70     90
      110    60
      30     20", header = TRUE)

Then, I create a new data frame, where the first row is repeated for each one of the other rows. A column grp is added, that links each of the repetitions of the first row to one of the other rows:

n <- nrow(df) - 1
new_data <- data.frame(X = c(rep(df$X[1], n), df$X[-1]),
                       Y = c(rep(df$Y[1], n), df$Y[-1]))
new_data$grp <- as.factor(rep(1:n, times = 2))
new_data
##     X  Y grp
## 1 100 50   1
## 2 100 50   2
## 3 100 50   3
## 4 100 50   4
## 5  80 60   1
## 6  70 90   2
## 7 110 60   3
## 8  30 20   4

Now, the plot can be created directly with ggplot:

library(ggplot2)
ggplot(new_data, aes(X, Y, group = grp)) + 
   geom_point() + geom_line()

enter image description here

The aesthetic group controls which points should be connected by a line. Since the column grp in new_data alway pairs a repetition of the first row with each of the other rows, the point corresponding to the first row is connected to each of the other points.

If you omit group = grp, a plot with a single line going through all the points is drawn.

like image 84
Stibu Avatar answered Oct 02 '22 00:10

Stibu